1.截取某個(gè)字符串前面的內(nèi)容:
varstr = "aaabbbcccdddeeefff";
tr = str.match(/(\S*)fff/)[1];
alert(str);//結(jié)果aaabbbcccddd
2.截取某個(gè)字符串后面的內(nèi)容:
varstr = "aaabbbcccdddeeefff";
str = str.match(/aaa(\S*)/)[1];
alert(str);//結(jié)果bbbcccdddeeefff
3.截取兩個(gè)字符串之間的內(nèi)容:
varstr = "aaabbbcccdddeeefff";
str = str.match(/aaa(\S*)fff/)[1];
alert(str);//結(jié)果bbbcccdddeee
4.用例:從Bilibili視頻鏈接中截取BV號(hào)
//普通鏈接
varstr = "https://www.bilibili.com/video/BV1vW411q7mj";
str = str.replace(/\//g,''); //去除鏈接中的斜杠
str = str.match(/vedio(\S*)/)[1]; //截取字符串"vedio"后的內(nèi)容
alert(str);//結(jié)果BV1vW411q7mj
//「稍后再看」鏈接
varstr = "https://www.bilibili.com/list/watchlater?bvid=BV1vW411q7mj&oid=1145141919";
str = str.match(/bvid=(\S*)&oid/)[1]; //截取字符串"bvid="與"oid"之間的內(nèi)容
alert(str);//結(jié)果BV1vW411q7mj
參考來源:
JS 正則截取字符串 - 義美-小義 - 博客園
js正則去除雙引號(hào)和斜杠_周達(dá)的博客-CSDN博客_js去掉斜杠文章來源:http://www.zghlxwxcb.cn/news/detail-692512.html
js將字符串中所有反斜杠\替換成正斜杠/_Name is Q的博客-CSDN博客_js字符串替換斜杠文章來源地址http://www.zghlxwxcb.cn/news/detail-692512.html
到了這里,關(guān)于JavaScript丨使用正則截取字符串內(nèi)容的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!