收藏
回答

无法使用正则表达式 (?<!)

代码

// 去除样式中top|right|bottom|left
// 使用正则匹配
// ASCII表 | \55 - | \72 : | \73 ; |

let str = 'margin-top:12px;margin-bottom:12px;top:0;left:13px;';

str.replace(/(?<!\55)(top|right|bottom|left)\72\w+\73/g, '')


报错



当前解决方案

// 去除样式中top|right|bottom|left
// 使用正则匹配
// ASCII表 | \55 - | \72 : | \73 ; |
let str = 'margin-top:12px;margin-bottom:12px;top:0;left:13px;';
str.replace(/(\w+\55)?(top|right|bottom|left)\72\w+\73/g, function(res){
 if (/(\w+\55)(top|right|bottom|left)/.test(res)) {
   return res
 } else {
   return ''
 }
})


期望解决方案

只用一个正则表达式就能完成正则匹配

最后一次编辑于  2017-12-27
回答关注问题邀请回答
收藏

7 个回答

  • 阿细
    阿细
    2017-12-27

    内个...好像这个表达式有问题呢



    目前我的解决方案是有,但我想一行代码解决。

    // 去除样式中top|right|bottom|left

    // 使用正则匹配

    // ASCII表 | \55 - | \72 : | \73 ; |

    let str = 'margin-top:12px;margin-bottom:12px;top:0;left:13px;';

    str.replace(/(\w+\55)?(top|right|bottom|left)\72\w+\73/g, function(res){

      if (/(\w+\55)(top|right|bottom|left)/.test(res)) {

       return res

      } else {
        return ''
      }
    })


    2017-12-27
    有用
    回复
  • 漫无止境
    漫无止境
    2017-12-27

    js目前还不支持负向的(?<=exp)和(?<!exp),不过有个提案,参见proposal-regexp-lookbehind

    http://2ality.com/2017/05/regexp-lookbehind-assertions.html

    https://v8project.blogspot.hk/2016/02/regexp-lookbehind-assertions.html

    2017-12-27
    有用
    回复
  • 红鲤鱼与绿鲤鱼与驴🐟🐟🐟
    红鲤鱼与绿鲤鱼与驴🐟🐟🐟
    2017-12-27

    let str2 = 'margin-top:12px;margin-bottom:12px;top:0;left:13px;';

        var a = str2.replace(/(<!\55top|right|bottom|left)\72\w+\73/g, '')  

    试下这个


    2017-12-27
    有用
    回复
  • 阿细
    阿细
    2017-12-27

    ...=w=,我可以啊~



    而且这个正则表达在百度百科里有(https://baike.baidu.com/item/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/1700215?fr=aladdin



    2017-12-27
    有用
    回复
  • 红鲤鱼与绿鲤鱼与驴🐟🐟🐟
    红鲤鱼与绿鲤鱼与驴🐟🐟🐟
    2017-12-27


    我在谷歌上也是不可以的

    2017-12-27
    有用
    回复
  • 阿细
    阿细
    2017-12-27
    • 在谷歌浏览器中是可以正常执行的

    • 在`开发者工具`中复制以上代码执行是不行的

    2017-12-27
    有用
    回复
  • 红鲤鱼与绿鲤鱼与驴🐟🐟🐟
    红鲤鱼与绿鲤鱼与驴🐟🐟🐟
    2017-12-27

    试了一下,是可以使用正则的,应该是你语法有错

    2017-12-27
    有用
    回复
登录 后发表内容