我正在寻找一个正则表达式来捕获具有以下属性的第一个子字符串:
例如,我想捕捉" FOO BAR"在以下
" ...这是文字... \ n ... ...我不关心...... \ n但我确实关心... FOO BAR ...... \ nNothing否则很重要。"
答案 0 :(得分:-1)
使用此:
// This will target only capitol letters and numbers and spaces
// it will also capture the first occurrence only
/\.\.\.[A-Z0-9 ]+\.\.\.\n/
以下是一个示例用法:
var string = "...this is TEXT...\n that...\nI DON'T CARE ABOUT...\nbut I do care about...FOO BAR...\nNothing else matters.";
var regex = new RegExp(/\.\.\.[A-Z0-9 ]+\.\.\.\n/);
var res = regex.exec(string);
var result = res[0].substring(3, res[0].length - 4); // strip out the ... and \n
console.log(result);