jquery - How should I use the RegExp to get my want in javascript? -
i tried 3 hours. face problem using javascript regexp.
i have string below
var str = 'test123↵↵http://www.youtube.com/watch?v=6w89smj25ni';
i use regexp url match, want other text except url.
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]); // want 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);
how about, once you've isolated url, replacing '' in text?
e.g.:
var url = re.exec(s)[1] + re.exec(s)[3] + re.exec(s)[5] console.log(s.replace(url,''));
Comments
Post a Comment