473,486 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Regexp - global replace of a character between tags

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
Mar 29 '07 #1
3 2563
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)
Mar 29 '07 #2
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
Mar 29 '07 #3
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)
Mar 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2547
by: Patryk Konieczka | last post by:
Hello Here's the thing I have a database edited by some company workers editing descriptions of books in the sotre , unfortunately these workers do not have the habit of inserting a space...
4
24719
by: McKirahan | last post by:
How would I use a regular expression to remove all trailing Carriage Returns and Line Feeds (%0D%0A) from a textarea's value? Thanks in advance. Also, are they any great references for learning...
20
3507
by: RobG | last post by:
I'm messing with getPropertyValue (Mozilla et al) and currentStyle (IE) and have a general function (slightly modified from one originally posted by Steve van Dongen) for getting style properties:...
19
3526
by: Dr Clue | last post by:
I'm not really an expert with RegExp() , although I do use it. The problem I have is that I want to strip comments out of a CSS file using RegExp() The reason is that I'm loading and parsing to...
6
4492
by: jiing24 | last post by:
I try to use regexp to replace some html tags in DOM, but the result seems some problems, ================================ <Script language="javascript" type="text/javascript"> var config =...
0
1391
by: deathtospam | last post by:
I have a value, retrieved from a recordset, that contains any number of hyperlinks to a number. For example: <a href="123456">Hi</a<a href='334455'>Hola</a> What I need to do is parse the...
2
1550
by: CJM | last post by:
I'm trying to validate and input field that allows an integer or a single '*' character using regular expressions, but I'm not getting the results I'm expecting. I'm trying to match against the...
11
2897
by: HopfZ | last post by:
I coudn't understand some behavior of RegExp.test function. Example html code: ---------------- <html><head></head><body><script type="text/javascript"> var r = /^https?:\/\//g;...
6
2253
by: runsun pan | last post by:
Hi I am wondering why I couldn't get what I want in the following 3 cases of re: (A) var p=/(+-?+):(+)/g p.exec("style='font-size:12'") -- // expected
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7126
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4865
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.