Page 1 of 1

Replace..

Posted: Sun Jul 26, 2009 5:16 am
by pcoder
Hi All,
I have a string like this:

Code: Select all

 
<input id=name>
<input id=address>
 
And I want to replace this string using regular expression to:

Code: Select all

 
<input id="name">
<input id="address">
 
Can anyone help me on this?
Thanks

Re: Replace..

Posted: Sun Jul 26, 2009 5:50 am
by prometheuzz
Try this:

PHP-regex pattern:

Code: Select all

'/id=([^"\s>]++)/'
Replacement string:

Code: Select all

id="$1"

Re: Replace..

Posted: Sun Jul 26, 2009 11:50 pm
by pcoder
Hi,
Thanks prometheuzz for your reply. I have not used regular expression yet like this.
I used your expression. It didn't work. And also I tried a lot of other techniques, still not working.
I have used this expression:

Code: Select all

 
var content = "<input id=name>
<input id=address>";
var re = '(\id=[A-Za-z0-9_]*)';
var myRegex = new RegExp(re,"g");
 
And It matches the exact string. And for the replace one, I think this one will work.

Code: Select all

 
var regReplace = (\id=\"[A-Za-z0-9_]*\");
 
But ,How can get the exact output like this:

Code: Select all

<input id="name"><input id="address">
Please guide me on this.

Thanks in advance

Re: Replace..

Posted: Mon Jul 27, 2009 1:00 am
by prometheuzz
pcoder wrote:Hi,
Thanks prometheuzz for your reply. I have not used regular expression yet like this.
I used your expression. It didn't work. ...
You're welcome. You should have mentioned that you were using JavaScript (note that this forum is PHP+regex). JavaScripts regex engine does not support possessive quantifiers and the delimiters I wrapped around the regex are used in PHP, not JavaScript. My regex works fine otherwise:

Code: Select all

id=([^"\s>]+)
with the same replacement string.