1. Intercept the content in front of a string:
varstr = "aaabbbcccdddeeefff";
tr = str.match(/(\S*)fff/)[1];
alert(str);//结果aaabbbcccddd
2. Intercept the content behind a certain string:
varstr = "aaabbbcccdddeeefff";
str = str.match(/aaa(\S*)/)[1];
alert(str);//结果bbbcccdddeeefff
3. Intercept the content between two strings:
varstr = "aaabbbcccdddeeefff";
str = str.match(/aaa(\S*)fff/)[1];
alert(str);//结果bbbcccdddeee
4. Use case: intercept BV number from Bilibili video link
//普通链接
varstr = "https://www.bilibili.com/video/BV1vW411q7mj";
str = str.replace(/\//g,''); //去除链接中的斜杠
str = str.match(/vedio(\S*)/)[1]; //截取字符串"vedio"后的内容
alert(str);//结果BV1vW411q7mj
//「稍后再看」链接
varstr = "https://www.bilibili.com/list/watchlater?bvid=BV1vW411q7mj&oid=1145141919";
str = str.match(/bvid=(\S*)&oid/)[1]; //截取字符串"bvid="与"oid"之间的内容
alert(str);//结果BV1vW411q7mj
Reference source:
JS regular intercept string - Yimei - Xiaoyi - blog garden