js 正则匹配标签,支持换行

这篇博客探讨了如何使用正则表达式来匹配和处理HTML标签,特别是在处理带有换行的复杂标签结构时。示例中展示了针对`<image>`和`<div>`标签的匹配和操作,为前端和后端开发者提供了关于字符串处理的实用技巧。
    let name = `
<div>
<image src='aaaa'></image>
    <image src='1111'></image>
    <image src='222'></image>
    <image src='33333'></image>
        <image 
        src='3333'
        ></image>
</div>
`;

// 正则匹配标签,支持换行
console.log(name)
    let uw = name.replace(/<image[^>]*?><\/image>/g, function (word) {
        console.log(word)
        //   return word.substring(0,1).toUpperCase()+word.substring(1);
    }
    );

第二种案例

    let name = `
<div
sss
>
<image src='aaaa'></image>
    <image src='1111'></image>
    <image src='222'></image>
    <image src='33333'></image>
        <image 
        src='3333'
        ></image>
</div>
<div></div>
`;

// 正则匹配标签,支持换行
// console.log(name)
    let uw = name.replace(/<div[^>]*?>([^]*)<\/div>/g, function (word) {
        console.log(word)
        //   return word.substring(0,1).toUpperCase()+word.substring(1);
    }
    );

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值