Solved

regular expressions

Posted on 2008-10-27
11
305 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Performance for Office 365 6 50
Jquery GMAP3 Hierarchy 1 28
Grunt script for Build Process 1 29
Javascript the "if condition with Or" 8 19
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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 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)

832 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