如果我有以下字符串:
• This is string 1<br />• This is string 2<br />• This is string 3<br />
如何获取•
和<br />
之间的所有内容,以便我最终获得:
["This is string 1", "This is string 2", "This is string 3"]
由于
答案 0 :(得分:1)
试试这个:
s.replace(/(^•\s*|<br(?: ?\/)?>)/g, "").split(/•\s*/g);
简单但有效,假设字符串始终由“&amp; bull;&lt; br /&gt;”分隔序列。
答案 1 :(得分:0)
您需要使用capturing groups(因为js不支持lookbehind断言)。
•\s*(.*?)(?=<br\s*\/>)
(?=<br\s*\/>)
肯定前瞻,断言匹配必须跟<br>
字符串。 \s*
匹配零个或多个空格字符。
从组索引1中获取所需的字符串。
> var s = []
undefined
> var f = "• This is string 1<br />• This is string 2<br />• This is string 3<br />"
undefined
> var re = /•\s*(.*?)(?=<br\s*\/>)/g;
undefined
> var m;
undefined
> while ((m = re.exec(f)) != null) {
... s.push(m[1]);
... }
3
> console.log(s)
[ 'This is string 1', 'This is string 2', 'This is string 3' ]
答案 2 :(得分:0)
•\s*(.*?)<br\s*\/>
您只需capturing
所需的群组即可。请参阅演示。
https://regex101.com/r/tX2bH4/23
var re = /•\s*(.*?)<br\s*\/>/gi;
var str = '• This is string 1<br />• This is string 2<br />• This is string 3<br />';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
答案 3 :(得分:0)
试试这个:
var a='• This is string 1<br />• This is string 2<br />• This is string 3<br />';
var r=a.split(/•\s*|<br\s*\/>/);
var nr=r.filter(function(v){return v!==''});
console.log(nr);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;