RegExp从模板中提取文本

时间:2016-05-19 14:46:35

标签: javascript regex

我有关于正则表达式和javascript的问题。用户将输入以下模板作为文本(它将根据用户的选择而改变),例如:

Accessories <accessory type> for a Group, for <What Purpose>

现在,用户将修改输入文本,并让它说这样结束。

我的电脑 我的电脑的我的打印机附件

我想知道我是否可以从该文本中提取出来的值,如果是这样的话,那将是我的电脑&#39;。有些情况下我会完全修改文本,但我现在可以取消等式。

可以用正则表达式完成吗?还是有什么东西可以帮我解决这个问题?

谢谢和问候,

2 个答案:

答案 0 :(得分:1)

var text = 'Accessories <accessory type> for a Group, for <What Purpose>';
var result = text;

var params =
{
        '<accessory type>': 'of my printer',
        '<What Purpose>': 'my computer'
};

var m = text.match(/<[^>]+>/g);

for(var i in m)
{
        var re = new RegExp(m[i], 'g');
        result = result.replace(re, params[m[i]]);
}

console.log(result);

DEMO:https://jsfiddle.net/ka4965uh/

结果: 我的打印机配件,用于我的电脑组

答案 1 :(得分:0)

描述

^Accessories\s+(?:of\s)?(.*?)\s+for\sa\sGroup,\sfor\s+(.*)\s*$

Regular expression visualization

此正则表达式将执行以下操作:

  • 捕获accessories
  • 忽略of
  • 之前的子字符串accessories
  • 捕获for子字符串值
  • 删除捕获值周围的任何空白

捕获论坛

  • 捕获组0获取整个字符串
  • 捕获组1获取accessories
  • 捕获组2获取for

实施例

现场演示

https://regex101.com/r/dB0aK5/1

示例文字

Accessories <accessory type> for a Group, for <What Purpose>
Accessories of my printer for a Group, for my computer

样本匹配

[0][0] = Accessories <accessory type> for a Group, for <What Purpose>
[0][1] = <accessory type>
[0][2] = <What Purpose>

[1][0] = Accessories of my printer for a Group, for my computer
[1][1] = my printer
[1][2] = my computer

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  Accessories              'Accessories'
----------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
----------------------------------------------------------------------
    of                       'of'
----------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
  )?                       end of grouping
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  for                      'for'
----------------------------------------------------------------------
  \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
  a                        'a'
----------------------------------------------------------------------
  \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
  Group,                   'Group,'
----------------------------------------------------------------------
  \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
  for                      'for'
----------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of a
                           "line"
----------------------------------------------------------------------