Solved

regular expressions

Posted on 2008-10-27
11
307 Views
Last Modified: 2012-05-05
hello experts,
How would I write a regular expression to check for a string of the following formats;
 '[number]>' and 'id='

The 'id=' would need to check for the following strings;-
id=blue
id=red
id=clue

The 'number]>' regex would need to check for the following strings;-
yui-rec0>
yui-rec1>
yui-rec2>
....

I'm programming in javascript.

Thanks.

0
Comment
Question by:claracruz
  • 6
  • 3
  • 2
11 Comments
 
LVL 16

Expert Comment

by:sh0e
ID: 22811713
str='id=blue';
alert( /id=/.test('str') );
str='yui-rec0>';
alert( /[0-9]>/.test('str') );
0
 
LVL 16

Expert Comment

by:sh0e
ID: 22811719
Sorry, typo.
str='id=blue';
alert( /id=/.test(str) );
str='yui-rec0>';
alert( /[0-9]>/.test(str) );

Open in new window

0
 
LVL 18

Expert Comment

by:Pawel Witkowski
ID: 22811950
I'm not sure is "id=" needed by claracruz or the thing after id, if  you need to retrieve value "blabla" in string "id=blabla" then this regexp should do the trick:

alert("id=blabla".match(/id\=(.+?)/)[1])

this is selecting regexp means that if string contains id=   it selects  text that is after "id=".

The same is with number:

alert("yui-rec034>".match(/(\d*)+>/)[1])

This will make sure that you select all numbers (not just 0-9 but  for ex. 345) that are before > 

[1] in both case mean to use selected element not just whole match.

Hope it helps
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!

 
LVL 18

Expert Comment

by:Pawel Witkowski
ID: 22811957
there was little mistake in second function, but should work too. this one is ok:

alert("yui-rec034>".match(/(\d*)>/)[1])

0
 
LVL 18

Expert Comment

by:Pawel Witkowski
ID: 22811979
Ahh and if u like to keep things clean you can use this version:

alert("yui-rec034>".match(/\d*(?=>)/))
0
 
LVL 4

Author Comment

by:claracruz
ID: 22812089
thanks Guys,

What I'm actually trying to do, is check ofr id=whatever1>, then do a replace so that it becomes id="whatever1">

My thinking now on seeing ur solutions is that perhaps its best to tell exactly what I want as there might be a straight forward way to achieve this with regex...

Any ideas...

0
 
LVL 18

Accepted Solution

by:
Pawel Witkowski earned 300 total points
ID: 22812286
Just try this one


alert("some test with <id=whatever1>".replace(/id\=(.+?)\>/g;, 'id="$1">');
0
 
LVL 4

Author Comment

by:claracruz
ID: 22812427
that worked excellent.

Just one final one pls..
How would I make this one
class=yui-dt-liner

become
class="yui-dt-liner"

When I do the following;-
s = (s.replace("class=yui-dt-liner", 'class="yui-dt-liner"'));

Only one instance of the occurrence is replaced.

I have increased the number of points for the additional Q.

Mnay thanks..
0
 
LVL 18

Expert Comment

by:Pawel Witkowski
ID: 22812597
the same but you need this  /g  on end of regexp meaning that this is global search (few times, not just first occurence):

s = (s.replace(new RegExp("class=yui-dt-liner","g"), 'class="yui-dt-liner"'));


I wrote a new RegExp  because i would need to escape special characters with  \  that would make this code less readable. Hope it helps
0
 
LVL 18

Expert Comment

by:Pawel Witkowski
ID: 22812660
less readable form:

s.replace(/class\=yui\-dt\-liner/g, 'class="yui-dt-liner"'))



this one should work too, but im curious if it work on every javascript engine:

alert((s.replace(/class=yui-dt-liner/g, 'class="yui-dt-liner"')))
0
 
LVL 4

Author Closing Comment

by:claracruz
ID: 31510256
superb!!!
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

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. …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

685 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