472,782 Members | 1,274 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 software developers and data experts.

help with string replace - for doing selective replace

Hello

I wish to replace all the characters in a string except those which
are inside '<' & '>' characters. And there could be multiple
occurences of < & > within the string.

e.g. string = "this is an example of <how> many words could be hidden
<under> these characters"

now, from this string all the characters should be searched & replaced
by a certain logic except <how> & <under>

I am accepting the string from a textarea form field and there can be
no or multiple occurences of words within < >

Any help will be highly appreciated.
Jul 23 '05 #1
4 1856
Prasad S wrote on 20 aug 2004 in comp.lang.javascript:
I wish to replace all the characters in a string except those which
are inside '<' & '>' characters. And there could be multiple
occurences of < & > within the string.

e.g. string = "this is an example of <how> many words could be hidden
<under> these characters"


s = "an example of <how> words <under> these characters"

s = s.replace(/[^<]*(<[^>]*>)[^<]*/g,'$1')
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #2
Prasad S wrote on 26 aug 2004 in comp.lang.javascript:
"Evertjan." <ex**************@interxnl.net> wrote in message
news:<Xn********************@194.109.133.29>...

s = "an example of <how> words <under> these characters"
s = s.replace(/[^<]*(<[^>]*>)[^<]*/g,'$1')


Thanks for a very prompt response! I tried your suggestion, its
retruning the following:

s = "an example of <how> words <under> these characters"
s = s.replace(/[^<]*(<[^>]*>)[^<]*/g,'$1')
result: <how>, <under>

However, i wish to have the following result:
e.g. i want to replace all 'a' s with 'Z' in the string which is not
within < & >
s = "an example of <how many> words <as good as> these characters"
RESULT should be: "Zn exZmple of <how many> words <as good as> these
chZrZcters"
i.e. it should replace all the a's which are not inside the < & > with
Zs


<script type="text/javascript">

Sin="an example of <how many> words <as good as> these characters"

Sout = ""
outside = true
for (i=0;i<Sin.length;i++) {
x = Sin.substr(i,1)
if (x=='<')outside = false
if (x=='>')outside = true
if (x=='a'&&outside) x = 'Z'
Sout+= x
}
alert(Sout)

</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #3
pr*********@rediffmail.com (Prasad S) wrote in message news:<be**************************@posting.google. com>...
Hi Evertjan

Thanks for a very prompt response! I tried your suggestion, its
retruning the following:
s = "an example of <how> words <under> these characters"

s = s.replace(/[^<]*(<[^>]*>)[^<]*/g,'$1')

result: <how>, <under>

However, i wish to have the following result:

e.g. i want to replace all 'a' s with 'Z' in the string which is not
within < & >

s = "an example of <how many> words <as good as> these characters"

RESULT should be: "Zn exZmple of <how many> words <as good as> these
chZrZcters"

i.e. it should replace all the a's which are not inside the < & > with
Zs

I have tried several different ways, still not able to crack this....

Any help will be useful....

"Evertjan." <ex**************@interxnl.net> wrote in message news:<Xn********************@194.109.133.29>...

s = "an example of <how> words <under> these characters"

s = s.replace(/[^<]*(<[^>]*>)[^<]*/g,'$1')

I don't know that this can be done in one line of code. I can do it
in two lines. I'm too busy to write the syntax now, however, here is
my idea.

You can use regular expressions to SPLIT the original string into an
array of strings, splitting by <.*> (in other words, splitting the
string by <how> and <under> in your example, creating 3 substrings).

Then it's a simple replace statement for each substring of the
array.
Jul 23 '05 #4
JRS: In article <be**************************@posting.google.com >,
dated Fri, 20 Aug 2004 06:12:36, seen in news:comp.lang.javascript,
Prasad S <pr*********@rediffmail.com> posted :

I wish to replace all the characters in a string except those which
are inside '<' & '>' characters. And there could be multiple
occurences of < & > within the string.


Can the string be of the form " x < y < z > a > ? ", If so, some record
of depth-of-quote is called for.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5

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

Similar topics

8
by: Foxy Kav | last post by:
Hi everyone, Im currently doing first year UNI, taking a programming course in C++, for one project i have to create a simple array manipulator... that i have done, but i cant figure out how to...
7
by: VMI | last post by:
If I have the string "Héllo", how can I replace char (é) with an 'e'? I cannot use the String.Replace() fuction. It has to be by replacing one char with another. Thanks.
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
1
by: intrader | last post by:
I have a .NET interop assembly Hash.MD5Sum with two methods Identity and GetMD5Sum. I want to call the methods from ASP (JScript), The debugger tells me that object oMD5Sum has one the ToString()...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
31
by: Extremest | last post by:
I have a loop that is set to run as long as the arraylist is > 0. at the beginning of this loop I grab the first object and then remove it. I then go into another loop that checks to see if there...
4
by: Sjaakie | last post by:
I need to replace parts of a string, in a collection deserialized from an XML file, with values from another collection. Is there another, more clever/faster/better, method than the loops below? ...
3
by: Matthew Warren | last post by:
I have the following piece of code, taken from a bigger module, that even as I was writing I _knew_ there were better ways of doing it, using a parser or somesuch at least, but learning how wasn't...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.