?
Solved

Regular Expressions and Substitution

Posted on 2004-09-02
10
Medium Priority
?
238 Views
Last Modified: 2008-02-01
I want to take a string that contains multiple form components and replace the name descriptor to be the name followed by an expression. For example, I want to change the following part of every component description:

name= 'someName'

to

name='someName' + aNumber  

where aNumber is a variable (integer). I want this to work on at least the major browsers.

Two ways I've considered is if there is a way to update the descriptor of a form component or the use of regular expressions. I don't really care how, I just can't seem to resolve the problem. For instance, when using replace, how can I tell it to "append" to the regular expression item that it found?

The key issue I am having is determining a simple way to use the string the regular expression just found. I believe this is the regular expression I want:

/name='\w*'/   but replace only seems to take a specific string as the second argument. Basically I just need to know if there is some reference to the expression I just found. I've tried $0 and $1, both are null.

Thanks for any quick help!

 

Thanks for any help!
 
0
Comment
Question by:kls1
  • 5
  • 3
  • 2
10 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 11967331
Show the source text string and show the resulting text string and I can show you the RegExp for doing it.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 11967359
And if you do not want to change a string, but only want to change the form element names on the page, then you need no RegExp.
By the way, changing the form input elements name is possible, but it is a bad idea.

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 11967417
To see the values of input fields someName1 to someName3 on the actual form do it like this:
<script>
function showValues(theBtn){
  theForm = theBtn.form;
  for(i=1;i<=3;i++){
     alert(theForm["someName"+i].value);
  }
}
</script>
<body>
<form>
  <input type=text name="someName1">
  <input type=text name="someName2">
  <input type=text name="someName3">
  <input type=button value="Show" onClick="showValues(this)">
</form>
</body>

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kls1
ID: 11967587
An example text string would be something like:

String someString = "<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>"

This is generated using another language and being sent to Javascript. I need to "append" numbers to the names of the fields since I'm using dynamic html to automatically generate "copies" of the template. So, I without appending the numbers, I would end up with something that looks like:


some String = "<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>
<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>
<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>
<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>"

(I added the newlines for clarity)

There can be an infinite number of elements in the original string and there can be and infinite number of "copies". Therefore, I either need to be able to actually access the name property and change it or I need to use regular expressions to change it.

Thanks again for any help!
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 1000 total points
ID: 11967768
Like this:

<script>

someString = "<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>\
<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>\
<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>\
<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>"

var rowNum = 0;
alert(someString.replace(/someName|anotherName|aName/g, function(p1){if(p1=="someName")rowNum++;return p1+rowNum}));

</script>

0
 
LVL 4

Assisted Solution

by:chipple
chipple earned 1000 total points
ID: 11970472
Example:

var html = "<input name='test'>";
var rx = new RegExp("(name='\\w+)(')");
var number = "0";
html = htlm.replace(rx,"$1"+number+"$2"));

Good luck!
0
 

Author Comment

by:kls1
ID: 11973237
Thanks! I was able to take your suggestions and use regular expressions to figure out howt o get this to work. I appreciate it!
0
 

Author Comment

by:kls1
ID: 11973347
I'm going to split the points on this. I can create a dummy question if you think that your contribution is worth more than I alot.

Thanks for the help, I appreciate it.
0
 
LVL 4

Expert Comment

by:chipple
ID: 11973400
I don't care so much for points, I'm glad if it helped. :)

Good luck!
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 11973403
You are welcome.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
Suggested Courses

850 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