Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Regexp - global replace of a character between tags

Reply
Thread Tools

Regexp - global replace of a character between tags

 
 
MB
Guest
Posts: n/a
 
      03-29-2007
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
 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      03-29-2007
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]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
 
MB
Guest
Posts: n/a
 
      03-29-2007
>> 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
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      03-29-2007
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]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
How can I replace all occurrences of a character with another character in std string? herman C++ 5 08-30-2007 09:05 AM
how to use regexp to replace some html tags in DOM jiing24@gmail.com Javascript 6 01-27-2006 02:12 PM
replacing tags between tags beartiger@gmail.com Perl Misc 9 09-19-2005 02:32 AM
RegExp between tags Charles Taylor ASP .Net 2 04-08-2005 01:06 PM



Advertisments