/    Sign up×
Community /Pin to ProfileBookmark

problem with replace and regularexpression

Hello

I have following syntax in a webpage:

.
.
var str=””;
str=document.getElementById(‘some_id’).value;
str=str.replace(/(<IM>)(.*)(</IM>)/g,”<b>bild:</b> $2″);
.
.

my variable ‘str’ gets correct inital value but if I in my textarea with id ‘some_id’ type:

<IM>picture1</IM>
<IM>picture2</IM>

the output vill be:

bild: picture1
picture2

i.e my regular expression does just take the first match even if I have set it with parameter ‘g’ that what I know means ‘look in the hole text’

I dont know what I’m missing and would be glad if anyone could explain this to me.

Regards /DS

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Killzone_KidJan 13.2008 — try this: str=str.replace(/<IM>(.*?)</IM>/g,"<b>bild:</b> $1");

It is mosty likely due to that .* grabs everything between the first tag till the last, so called "greedy" matching
Copy linkTweet thisAlerts:
@digital_stormauthorJan 13.2008 — try this: str=str.replace(/<IM>(.*?)</IM>/g,"<b>bild:</b> $1");

It is mosty likely due to that .* grabs everything between the first tag till the last, so called "greedy" matching[/QUOTE]


Thank you!

It works fine when I do as you suggesting :-)
×

Success!

Help @digital_storm spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...