r.sendecky
r.sendecky

Reputation: 10363

Replace multiple strings in text with reg ex

I have some text sprinkled with [cid:ce743cbf-8c44-4c84-aa72-6d2c9eb4b81a] labels (inline attachments)

I need to go through the text and replace it with the links to the file. So I need to match the actual label with the brackets and remember the UUID within to be given to the replace function as a parameter.

So given the string

var str = "some text 1 [cid:12345] more text follows 2 [cid:6789] last text"

I am trying to replace it with this test but it does not even match correctly.

str.replace(/\[cid:(.+)\]/gm, function(m,p){return p})

I don't know much about reg ex, please help. I reckon it is something to do with greediness as it consumes everything between the first match and the last.

Thanks.

Upvotes: 1

Views: 50

Answers (2)

Guffa
Guffa

Reputation: 700342

Yes, you are correct that is has to do with greediness. The .+ part will match everything to the end of the string, and then it will start to backtrack to match the bracket, finding the last bracket in the string.

Adding ? after the .+ will make it non-greedy:

var str = "some text 1 [cid:12345] more text follows 2 [cid:6789] last text"

str = str.replace(/\[cid:(.+?)\]/gm, function(m,p){return p});

// show in Stackoverflow snippet
document.write(str);

An alternative is to match everything except the closing bracket:

str = str.replace(/\[cid:([^\]]+)\]/gm, function(m,p){return p});

Upvotes: 2

hwnd
hwnd

Reputation: 70732

+ is a greedy operator meaning it will match as much as it can and still allow the remainder of the regular expression to match. Use +? for a non-greedy match meaning "one or more — preferably as few as possible".

/\[cid:(.+?)]/g

Note: You can remove the m (multi-line) modifier, it's meaningless to use here.

Upvotes: 2

Related Questions