正则表达式捕获具有以下属性的第一个子字符串

时间:2018-01-10 00:08:19

标签: regex regex-lookarounds

我正在寻找一个正则表达式来捕获具有以下属性的第一个子字符串:

  1. 子字符串不包含小写字母或符号
  2. 子字符串立即,前面带有" ..."
  3. 子字符串 immdiately 后跟" ... \ n
  4. 例如,我想捕捉" FOO BAR"在以下

    " ...这是文字... \ n ... ...我不关心...... \ n但我确实关心... FOO BAR ...... \ nNothing否则很重要。"

1 个答案:

答案 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);