2016-08-04 54 views
0

我有一個內容。我將這一段分成不同的行,以便我可以清楚地解釋。正則表達式包含標籤之間的

<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 

如何寫一個正則表達式,以識別與這樣的條件的話。請幫忙。

+0

你試過沒有? – Rao

+0

@Rao我對正則表達式很陌生。我試了一些,我猜這是不正確的https://regex101.com/r/wJ9rL3/1。它沒有標識所有的情況下,特別是有多個標籤 – chai

回答

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 
+0

的標籤這很好,但在https://regex101.com/r/cE4mE3/2在這種情況下,它必須返回3個匹配,即,你可以在這裏看到https://regex101.com/r/cE4mE3/3。在這個我分裂的內容讓你更好地理解 – chai

相關問題