Regexp - global replace of a character between tags

M

MB

I need to replace all occurances of a certain character located between
two tags. I have included an example of what i have come up with so far,
but it doesn't work they way I want it to:

str = "some content<script type=\"text/javascript\">var str =
'asdfasdfASDFASDF';<\/script>";
str = str.replace(/(<script.*?>.*?)a(.*?<\/script>)/gim, "$1X$2");
alert(str);

In this example, i want all 'a' between the script tags to be replaced
by 'X', but only the first is replaced. How can I modify this to replace
*all* occurances of 'a'?

/MB
 
E

Evertjan.

MB wrote on 29 mrt 2007 in comp.lang.javascript:
I need to replace all occurances of a certain character located between
two tags. I have included an example of what i have come up with so far,
but it doesn't work they way I want it to:

str = "some content<script type=\"text/javascript\">var str =
'asdfasdfASDFASDF';<\/script>";
str = str.replace(/(<script.*?>.*?)a(.*?<\/script>)/gim, "$1X$2");
alert(str);

In this example, i want all 'a' between the script tags to be replaced
by 'X', but only the first is replaced. How can I modify this to replace
*all* occurances of 'a'?

str = str.replace(/(<script.*?>)(.*)(?=<\/script>)/gi, function(x,y,z)
{return y+z.replace(/a/gi,'Z')})

[beware of line break. IE tested]
 
M

MB

I need to replace all occurances of a certain character located between
two tags. I have included an example of what i have come up with so far,
but it doesn't work they way I want it to:

str = "some content<script type=\"text/javascript\">var str =
'asdfasdfASDFASDF';<\/script>";
str = str.replace(/(<script.*?>.*?)a(.*?<\/script>)/gim, "$1X$2");
alert(str);

In this example, i want all 'a' between the script tags to be replaced
by 'X', but only the first is replaced. How can I modify this to replace
*all* occurances of 'a'?

str = str.replace(/(<script.*?>)(.*)(?=<\/script>)/gi, function(x,y,z)
{return y+z.replace(/a/gi,'Z')})

[beware of line break. IE tested]

Thanks. This works well, however only as long as the contents of 'str'
is just one line. It does not find any matches when I have several
lines. I tried adding the m-flag to the regular expressions, but that
didn't work. Can this be solved too?

/MB
 
E

Evertjan.

MB wrote on 29 mrt 2007 in comp.lang.javascript:
I need to replace all occurances of a certain character located
between two tags. I have included an example of what i have come up
with so far, but it doesn't work they way I want it to:

str = "some content<script type=\"text/javascript\">var str =
'asdfasdfASDFASDF';<\/script>";
str = str.replace(/(<script.*?>.*?)a(.*?<\/script>)/gim, "$1X$2");
alert(str);

In this example, i want all 'a' between the script tags to be
replaced by 'X', but only the first is replaced. How can I modify
this to replace *all* occurances of 'a'?

str = str.replace(/(<script.*?>)(.*)(?=<\/script>)/gi,
function(x,y,z) {return y+z.replace(/a/gi,'Z')})

[beware of line break. IE tested]

Thanks. This works well, however only as long as the contents of 'str'
is just one line. It does not find any matches when I have several
lines. I tried adding the m-flag to the regular expressions, but that
didn't work. Can this be solved too?


str = str.replace(/\n/g,'\uffff').replace(/(<script.*?>)(.*)(?=
<\/script>)/gi, function(x,y,z) {return y+z.replace(/a/gi,'Z')}).replace(/
\uffff/g,'\n')

[beware of line breaks. IE tested]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top