Replace..

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Replace..

Post 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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Replace..

Post by prometheuzz »

Try this:

PHP-regex pattern:

Code: Select all

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

Code: Select all

id="$1"
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Replace..

Post 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
Last edited by pcoder on Mon Jul 27, 2009 1:10 am, edited 1 time in total.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Replace..

Post 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.
Post Reply