2015-12-03 46 views
1

是否可以選擇所有單詞,即不是標籤而不是標籤內部的屬性?我已經得到了這個反作用,我知道我可以在兩個階段做到這一點,取代第一場比賽,並做出新的Javascript RegExp搜索。但事情是,我想用一種表達方式讓它工作。正則表達式 - 選擇不是標籤名稱或屬性的單詞

http://regexr.com/3cb6g

(<[^>]*>)|({[^>]*}) 

輸入:

<p>Test image captions for GitBook:</p> 

<p>Second image: <img scr="./image2.png" alt="image title" title="image title">asdf</img>{caption width="300" style="height:'300px'"} </p> 

<p>Sample text and first image: <img scr="./image1.png" alt="image 1" /> {caption width="300" style="height:'300px'"} for testing ok...</p> 

預計輸出標記內`應該匹配的單詞:

<p>`Test` `image` `captions` `for` `GitBook`:</p> 

<p>`Second` `image`: <img scr="./image2.png" alt="image title" title="image title">`asdf`</img>{caption width="300" style="height:'300px'"} </p> 

<p>`Sample` `text` `and` `first` `image`: <img scr="./image1.png" alt="image 1" /> {caption width="300" style="height:'300px'"} `for` `testing` `ok`...</p> 
+0

見http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-標籤 – Ultimater

+0

太糟糕了javascript/nodejs正則表達式不支持負面回送... – MarkokraM

+0

雖然JavaScript支持負向預覽。您可以嘗試顛倒您正在處理的字符串,並顛倒您匹配的文字字符的順序,然後以相反的方式使用前瞻。您可能會對http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript – Ultimater

回答

0

你可以試試這個:

var words = []; 
$(function() { 
    $("p").each(function() { 
    words.concat($(this).text().split(" ")); 
    }); 
}); 

現在words數組包含所有單詞。

0

嘗試使用.textContentString.prototype.replace()RegExp/\{.*\}|:|\.+|\s{2}|\s$/gi

var p = document.getElementsByTagName("p"), res = []; 
 
for (var text = "", i = 0; i < p.length; i++) { 
 
    res[i] = p[i].textContent.replace(/\{.*\}|:|\.+|\s{2}|\s$/gi, "") 
 
} 
 
console.log(res)
<!-- 
 
<p>`Test` `image` `captions` `for` `GitBook`:</p> 
 

 
<p>`Second` `image`: <img scr="./image2.png" alt="image title" title="image title">`asdf`</img>{caption width="300" style="height:'300px'"} </p> 
 

 
<p>`Sample` `text` `and` `first` `image`: <img scr="./image1.png" alt="image 1" /> {caption width="300" style="height:'300px'"} `for` `testing` `ok`...</p> 
 
--> 
 
<p>Test image captions for GitBook:</p> 
 

 
<p>Second image: <img scr="./image2.png" alt="image title" title="image title">asdf</img>{caption width="300" style="height:'300px'"} </p> 
 

 
<p>Sample text and first image: <img scr="./image1.png" alt="image 1" /> {caption width="300" style="height:'300px'"} for testing ok...</p>

1

我的問題,因爲答案是使用JavaScript代碼來處理比賽可能就沒有太清楚。我的目的是隻用簡單的表達方式找到解決方案。我終於找到了這表達的是滿足我的需求:

((?!([^<]+)?>)([\w]+)(?!([^\{]+)?\})([\w]+)) 

http://regexr.com/3cb6j

相關問題