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

jquery - Javascript Regex to replace brackets with content inside - Stack Overflow

programmeradmin6浏览0评论

I'm trying to build a regular expression to replace brackets with other content, in this case, a div tag..

So, the text:

This is a [sample] text

Would bee:

This is a <div>sample</div> text

The problem is, it should only replace when both brackets are found, and keep the content inside. If only one bracket is found, it should ignore it..

So

This is a [sample text

or

This is a ] sample text

Both would remain as is.

Also it should match more than one occurence, so:

This [is] a [sample] [text]

Would bee

This <div>is</div> a <div>sample</div> <div>text</div>

And one last thing, it should remove (or at least ignore) nested brackets, so

This [[is a ] sample [[[ tag]]]

Would bee

This <div>is a</div> sample <div> tag </div>

This is what I got until now:

function highlightWords(string){
    return string.replace(/(.*)\[+(.+)\]+(.*)/,"$1<div>$2</div>$3");
}

It works in simple cases, but won't get multiple occurences and won't remove other tags. Any regex masters around?

I'm trying to build a regular expression to replace brackets with other content, in this case, a div tag..

So, the text:

This is a [sample] text

Would bee:

This is a <div>sample</div> text

The problem is, it should only replace when both brackets are found, and keep the content inside. If only one bracket is found, it should ignore it..

So

This is a [sample text

or

This is a ] sample text

Both would remain as is.

Also it should match more than one occurence, so:

This [is] a [sample] [text]

Would bee

This <div>is</div> a <div>sample</div> <div>text</div>

And one last thing, it should remove (or at least ignore) nested brackets, so

This [[is a ] sample [[[ tag]]]

Would bee

This <div>is a</div> sample <div> tag </div>

This is what I got until now:

function highlightWords(string){
    return string.replace(/(.*)\[+(.+)\]+(.*)/,"$1<div>$2</div>$3");
}

It works in simple cases, but won't get multiple occurences and won't remove other tags. Any regex masters around?

Share Improve this question edited Jul 25, 2015 at 3:22 Bibek Sharma 3,3307 gold badges43 silver badges65 bronze badges asked Jul 25, 2015 at 3:11 sigmaxfsigmaxf 8,51215 gold badges70 silver badges133 bronze badges 2
  • regexr./3bfa6 - but not the nested one – Arun P Johny Commented Jul 25, 2015 at 3:22
  • Seems exactly a job for positive look-aheads – Downgoat Commented Jul 25, 2015 at 4:47
Add a ment  | 

1 Answer 1

Reset to default 8

No need to describe content before and after brackets. You must forbid brackets in the content description, so use [^\][]+ instead of .+. Don't forget to add g for a global replacement:

function highlightWords(string){
    return string.replace(/\[+([^\][]+)]+/g,"<div>$1</div>");
}

Note: you don't need to escape the closing square bracket outside a character class, it isn't a special character.

发布评论

评论列表(0)

  1. 暂无评论