我试了大约3个小时。我遇到了使用javascript regexp的问题。
我有一个字符串,如下所示
var str = 'test123↵↵http://www.youtube.com/watch?v=6W89sMJ25nI';
我使用正则表达式来获取网址匹配,但我还想获得除网址之外的其他文字。
var match = 'https://www.youtube.com/watch?v=6W89sMJ25nI';
var str = 'test123↵http://www.youtube.com/watch?v=6W89sMJ25nI';
var str2 = 'test123↵↵';
function checkStringContainUrl(s) {
var re = new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?");
if(re.test(s)) {
console.log("url inside");
console.log(re.exec(s)[1] + re.exec(s)[3] + re.exec(s)[5]);
// i also want to console 'test123↵↵'
} else {
console.log('no url inside');
}
}
function checkLink(match) {
var re = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|<\/a>))[?=&+%\w.-]*/ig;
var m = re.exec(match);
console.log(m[1]);
}
//checkLink(match);
checkStringContainUrl(str);
答案 0 :(得分:0)
如果您已将网址隔离,请将其替换为&#39;&#39;在文中?
e.g:
var url = re.exec(s)[1] + re.exec(s)[3] + re.exec(s)[5]
console.log(s.replace(url,''));