我有一个带有内联图片网址的richtext输入。此输入有时会被转义。我想找到所有具有 abc / ab?之类路径的网址。 我的测试https://regex101.com/r/sY4eX8/3
的链接regex : /src\s*=\s*\\?['"](https?)?.+?abc\/sd.+?\\?['"]/
Text : Hello check image1 src="https://abdcom/abc/sdp?a=a&ad=d" \n <br> image 2 src=\'/abc/sd\' image3 src='http:ab/abc/sdp' \n image 4 src='http://ab/abc/sdp?bc=a' image 5 src ='http//ab.com/abc/sdp' image 6src=\"./abc/sd\" and image 7 src=\"../abc/sd\"
问题出在regex101.com里面它显示了所有7个匹配但是当我尝试在java / javascript中运行它只显示5没有正确的url匹配,我缺少或做错了什么?
感谢。
答案 0 :(得分:1)
这应该适合你:
var re = /src\s*=\s*\\?(['"])(?:https?:\/\/)?(.*?abc\/sd.*?)\\?\1/gi;
var str = 'kkkkkkkkk src="https://abdcom/abc/sdp?a=a&ad=d\" \n <br> src=\"/abc/sd\" src=\'http://ab/abc/sdp\' \n ds src=\'http://ab/abc/sdp?bc=a\'fdsfsds \nsrc =\'http://ab.com/abc/sdp\' src=\"./abc/sd\" src=\"../abc/sd\"';
var m;
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex)
re.lastIndex++;
console.log(m[2]);
}
<强>输出:强>
abdcom/abc/sdp?a=a&ad=d
/abc/sd
ab/abc/sdp
ab/abc/sdp?bc=a
ab.com/abc/sdp
./abc/sd
../abc/sd