Solved

regular expressions

Posted on 2008-10-27
11
304 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Animated .jpg? 13 61
window.setInterval not firing 7 31
Split in Javascript 5 31
Wrapper for APPs 9 40
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now