最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript regex to remove string inside bracket - Stack Overflow

programmeradmin3浏览0评论

I'd like to remove character between { and }.

Example :

 input_string = "i like apple {nobody knows}";

expected result :

"i like aple"

I'd like to remove character between { and }.

Example :

 input_string = "i like apple {nobody knows}";

expected result :

"i like aple"
Share Improve this question edited Feb 1, 2013 at 14:26 Denys Séguret 382k90 gold badges810 silver badges775 bronze badges asked Feb 1, 2013 at 14:22 guurnitaguurnita 4575 gold badges11 silver badges22 bronze badges 1
  • There is an exact question the other day... – nhahtdh Commented Feb 1, 2013 at 14:23
Add a comment  | 

2 Answers 2

Reset to default 16

You can use

 var out = input_string.replace(/{[^}]*}/,'')

If you want to remove more than one occurrence, use

 var out = input_string.replace(/{[^}]*}/g,'')

To remove things between /* and */ , this one should work :

 var out = input_string.replace(/(?!<\")\/\*[^\*]+\*\/(?!\")/g,'')
let  regex = /<([^>]+)>/g;
      let match;
      let result = {}
      while ((match = regex.exec(content))){
        if(match && match[0] && match[1])
        result[match[0]] = match[1];
      }
      if(result && Object.keys(result).length){
        Object.entries(result).forEach(([key, value]) => {
          if(content.includes(key)){
            content = content.replace(key, value);
          }
        })
      }
发布评论

评论列表(0)

  1. 暂无评论