473,551 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ 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\">va r str =
'asdfasdfASDFAS DF';<\/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 2575
MB wrote on 29 mrt 2007 in comp.lang.javas cript:
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\">va r str =
'asdfasdfASDFAS DF';<\/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\">va r str =
'asdfasdfASDFA SDF';<\/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.javas cript:
>>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\">va r str =
'asdfasdfASDF ASDF';<\/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').rep lace(/(<script.*?>)(. *)(?=
<\/script>)/gi, function(x,y,z) {return y+z.replace(/a/gi,'Z')}).repla ce(/
\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
2553
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 character after word-ending dot, this is why i use the function: function getCorrect($txt) { $txt=str_replace(",",", ",$txt); $txt=str_replace(".","....
4
24733
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 how to use Regular Expressions?
20
3512
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: function GetCurrentStyle( el, prop ) { if ( window.getComputedStyle ) { // Mozilla et al return window.getComputedStyle(el,...
19
3536
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 simulate javscript access to stylesheets in Opera. I thought I had it licked untill the '/' characters in url('') tripped me up Below is a test...
6
4497
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 = document.getElementById("rootconfig"); alert(config.innerHTML);
0
1396
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 string, identify any such valid hyperlinks, look up each of those numbers in a database, and return a string that looks something like this: <a...
2
1555
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 pattern "*|", which I hoped would translate to 'match against groups of the numbers (0-9) or against a single *'. However, it's matching against...
11
2908
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; document.write( ); </script></body></html> ---------------------
6
2261
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
7565
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7522
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7847
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6106
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5406
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5130
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3534
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1097
muto222
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.