轻松推到正则表达式(五)递归及去空格

递归 |(?R)

广告
标题
中取到 标题
先去掉最外层的

取最少  (.+?)
$mach = "/
(.+?)<\/div>/";
广告 取最多 (.+) $mach = "/
(.+)<\/div>/"; string(23) "
广告
标题"

额外多出一些div 标签 干掉

$mach = "/
([^<>].+?)<\/div>/"; 得到 广告

取最少比较符合但是不要广告要标题怎么破 这个时候就要使用到递归了

$mach = "/
([^<>]+|(?R))*<\/div>/"; 得到 标题

去空格

字符串替换 str.replace('被替换掉的字符串','替换成的内容');
这里
'被替换掉的字符串' 如果是正则的话千万不要写' ' 否者不能正常使用 他会把你写的正则当做字符串来替换
空格在正则中用\s 表示

 var str = " hello world ";
//   str = str.replace(/^\s+/g,''); // 仅替换掉掉行首的空格
//    str = str.replace(/\s+$/g,'');//替换掉行末的空格
//str = str.replace(/\s+/g,'');// 同时干掉所有空格
    str = str.replace(/^\s+|\s$/g,'');//一句话同事替换掉行首与行末
//    alert(str.replace(/^\s+/g,''));
    console.log("--"+str+"--")

我是小菜鸡,我喂自己袋盐

你可能感兴趣的:(轻松推到正则表达式(五)递归及去空格)