// 截取两个字符串之间的内容:
function testMatch1(text) {
const str = text.match(/aaa(\S*)fff/)[1];
console.log('截取两个字符串之间的内容:', str);
}
// 截取某个字符串前面的内容
function testMatch2(text) {
const str = text.match(/(\S*)fff/)[1];
console.log('截取某个字符串前面的内容', str);//结果aaabbbcccddd
}
// 截取某个字符串后面的内容
function testMatch3(text) {
const str = text.match(/aaa(\S*)/)[1];
console.log('截取某个字符串后面的内容', str);
}
testMatch1('aaabbbcccdddeeefff');
testMatch2('aaabbbcccdddeeefff');
testMatch3('aaabbbcccdddeeefff');
function urlMatch(text) {
const matchs = text.match(/\:\/\/(\S*)\?/);
const match = matchs && matchs.length >= 1 && matchs[1];
console.log('url匹配:', matchs);
return match;
}
function urlMatch2(text) {
// 这个方法的特点是可以提取出协议名
const rep = /(http|https):\/\/([\w.]+\/?)\S*/;
const matchs = text.match(rep);
const match = matchs && matchs.length >= 1 && matchs[1];
console.log('url匹配:',match, matchs);
console.log('url匹配 is:',rep.test(text));
}
function urlMatch3(text) {
// 这个方法的特点是不提取出协议名;
const rep = /http[s]{0,1}:\/\/([\w.]+\/?)\S*/
console.log('url匹配:',rep.test(text));
}
const url1 = 'http://www.baidu.com?q=1111';
urlMatch(url1);
urlMatch('http://www.baidu.com/a/b/c/?e=1&c=http://2.com');
urlMatch('www.baidu.com/a/b/c/?e=1&c=http://2.com?e=1');
const str = `<img referrerpolicy="no-referrer" src="//static.doutula.com/img/loader.gif?33" data-original="http://img.doutula.com/production/uploads/image/2019/12/11/20191211048661_TUBLPS.jpg" alt="我把舞台给你" class="img-responsive lazy image_dta" data-backup="http://img.doutula.com/production/uploads/image/2019/12/11/20191211048661_TUBLPS.jpg">`;
const result = str.match(/data-original="(.+?)"/)[1];
console.log('str', result);
urlMatch2('https://www.baidu.com/a/b/c?e=1&c=http://2.com');
urlMatch3('https://www.baidu.com/a/b/c/?e=1&c=http://2.com');
console