mb2o
mb2o

Reputation: 35

Javascript regular expression to extract text from string

Let's say I have the following string:

[lorem]{lorem;ipsum;solor;sit;amet}[ipsum]<i>Lorem</i> ipsum <b>dolor</b> sit amet

What I want is an object that contains the following:

{lorem: "{lorem;ipsum;solor;sit;amet}", ipsum: "<i>Lorem</i> ipsum <b>dolor</b> sit amet"}

How would my regular expression look? Is there any way to get the inverse of this?

/\[\w+\]/g

Thanks...

Upvotes: 0

Views: 98

Answers (1)

Sam
Sam

Reputation: 20486

Instead of creating the inverse of /\[\w+\]/g, just use .split():

var string = '[lorem]{lorem;ipsum;solor;sit;amet}[ipsum]<i>Lorem</i> ipsum <b>dolor</b> sit amet'
console.log(string.split(/\[\w+\]/));

Demo

Upvotes: 1

Related Questions