Solved

Javascript and regular expression : parenthesis in a pattern

Posted on 2004-03-25
5
438 Views
Last Modified: 2006-11-17
Hi

I'm using Javascript and Regular Expression (through the regexp object) to dynamicaly modify a html page. I have a problem when I'm trying to use a pattern with parentheses:

I want this:
somestring(1)

to become:
somestring(2)

but parentheses have a proper use in regular expression.. here's my line:

html = html.replace(new RegExp('somestring(1)',"g"),'somestring(2)')

It just doesn't find the pattern. I tried using the backslash character to transform this special character in a literal character, but still nothing:

html = html.replace(new RegExp('somestring\(1\)',"g"),'somestring(2)')

It's drving me nuts!

Thanks for your time!
0
Comment
Question by:Cyrrus30
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 14

Expert Comment

by:ziffgone
ID: 10680810
Try removing the ' from around your something(1) and something(2)

html = html.replace(new RegExp(somestring(1),"g"),somestring(2))

Hope that works.
0
 
LVL 12

Accepted Solution

by:
venkateshwarr earned 100 total points
ID: 10680836
Here is an example

<script language="JavaScript">
     html = "somestring(1);;;;;;;somestring(1)"
     html = html.replace(/somestring\(1\)/gi,'somestring(2)')
     alert(html);
</script>
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10680904
try this

<script>
html='12345somestring(1)wxyz'
html.replace(/somestring\(1\)/gi,'somestring(2)')

alert(html)
</script>
0
 
LVL 1

Author Comment

by:Cyrrus30
ID: 10681450
Ok, your technique works great venkateshwarr, but there's a little detail missing. Sorry I didn't mention it in my question.

The '1' I want to change into a '2' is in fact a variable (index in a for loop). I tried to add this to your solution, but I didn't find the way. I hope it's possible!

So if you have an idea, tell me. And if you don't, I'll give the point to venkateshwarr as he answered my question.
0
 
LVL 1

Author Comment

by:Cyrrus30
ID: 10688309
Ok, I finaly find a way to insert a variable in my pattern. Here's what I did:

html = html.replace(new RegExp('somestring\\(' + I + '\\)',"g"),'somestring(2)')

It took my a while to figure otu how to concatenate using patterns. In the exemple you showed, the string where bounded by slashes to show that it was a pattern. But concatenating with that kind of string doesn't work quite well. So I had to use a normal string, but with this technique my backslashes used to diseapeared, until I tried the double backslashes. So the string:
'somestring\\(' + I + '\\)'
is in fact this (when I = 1):
somestring\(1\)
wich is exactly what you told me to use, so it's working!

Thanks again for you help!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Node.js 11 81
Video Tutorial help 2 51
javascript form - error when hitting enter 3 46
How to open a new browser tab after executing php script 20 49
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question