正则表达式中包含标签的单词

时间:2016-08-04 01:58:00

标签: javascript jquery regex

我有一个内容。我把段落划分为不同的行,以便我能够清楚地解释。

 <p>The most 
 i<del class="del" editid="6">m</del>por<ins class="ins">sss</ins>t<del class="del>a</del>n<ins class="ins">sss</ins>t 
 reso<del class="del">ur</del>ce 
 for all develo<ins class="ins">vvv</ins>pers 
 working with , 
 integratin<del class="del">g i</del>t
 with their 
 <ins class="ins">ssss</ins>w<ins class="ins">ss</ins><del class="del">e</del><ins class="ins">ss</ins>bsi<del class="del">te</del>s 
 and applications, 
 an<ins class="ins">sss</ins>d<del class="del"> </del>customizing
 to their needs. You can start from here. 

在这里,我的单词之间包含<del></del> and <ins></ins> tags。每个单词都有<del></del> and <ins></ins> tags个标签。

我正在尝试编写一个正则表达式,用于标识由<del></del> and <ins></ins> tags组成的单词。

对于注释,正则表达式仅突出显示<del></del> and <ins></ins> tags的单词。该字词可以以字母或<del>标记或<ins>标记开头,也可以以字母或<del>标记或<ins>标记结尾

这里的一个词意味着它必须在一个空格之后开始直到它找到一个空格(空间不在<del> and <ins> tags or space not between the words inside the <del></del> and <ins></ins> tags之间)

例如,在内容中,单词是

  i<del class="del" editid="6">m</del>por<ins class="ins">sss</ins>t<del class="del>a</del>n<ins class="ins">sss</ins>t 

  reso<del class="del">ur</del>ce

  integratin<del class="del">g i</del>t

  <ins class="ins">ssss</ins>w<ins class="ins">ss</ins><del class="del">e</del><ins class="ins">ss</ins>bsi<del class="del">te</del>s

  an<ins class="ins">sss</ins>d<del class="del"> </del>customizing

如何编写正则表达式来识别具有此类条件的单词。请帮忙。

1 个答案:

答案 0 :(得分:0)

Regex101

text = ['<p>The most '
    ,' i<del class="del" editid="6">m</del>por<ins class="ins">sss</ins>t<del class="del>a</del>n<ins class="ins">sss</ins>t '
    ,' reso<del class="del">ur</del>ce '
    ,' for all develo<ins class="ins">vvv</ins>pers '
    ,' working with , '
    ,' integratin<del class="del">g i</del>t'
    ,' with their '
    ,' <ins class="ins">ssss</ins>w<ins class="ins">ss</ins><del class="del">e</del><ins class="ins">ss</ins>bsi<del class="del">te</del>s '
    ,' and applications, '
    ,' an<ins class="ins">sss</ins>d<del class="del"> </del>customizing'
    ,' to their needs. You can start from here.'
].join('\n');
text.match(/(\s|^)(\S{0,}<(del|ins).*>(.*)<\/(del|ins)>\S{0,})(\s|$)/g);

结果:

Array 
0 " i<del class="del" editid="6">m</del>por<ins class="ins">sss</ins>t<del class="del>a</del>n<ins class="ins">sss</ins>t "
1 " reso<del class="del">ur</del>ce "
2 " develo<ins class="ins">vvv</ins>pers "
3 " integratin<del class="del">g i</del>t "
4 " <ins class="ins">ssss</ins>w<ins class="ins">ss</ins><del class="del">e</del><ins class="ins">ss</ins>bsi<del class="del">te</del>s "
5 " an<ins class="ins">sss</ins>d<del class="del"> </del>customizing "
length 6