Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

regular expressions

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
claracruz
Asked:
claracruz
  • 6
  • 3
  • 2
1 Solution
 
sh0eCommented:
str='id=blue';
alert( /id=/.test('str') );
str='yui-rec0>';
alert( /[0-9]>/.test('str') );
0
 
sh0eCommented:
Sorry, typo.
str='id=blue';
alert( /id=/.test(str) );
str='yui-rec0>';
alert( /[0-9]>/.test(str) );

Open in new window

0
 
Pawel WitkowskiSenior Javascript DeveloperCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Pawel WitkowskiSenior Javascript DeveloperCommented:
there was little mistake in second function, but should work too. this one is ok:

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

0
 
Pawel WitkowskiSenior Javascript DeveloperCommented:
Ahh and if u like to keep things clean you can use this version:

alert("yui-rec034>".match(/\d*(?=>)/))
0
 
claracruzAuthor Commented:
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
 
Pawel WitkowskiSenior Javascript DeveloperCommented:
Just try this one


alert("some test with <id=whatever1>".replace(/id\=(.+?)\>/g;, 'id="$1">');
0
 
claracruzAuthor Commented:
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
 
Pawel WitkowskiSenior Javascript DeveloperCommented:
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
 
Pawel WitkowskiSenior Javascript DeveloperCommented:
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
 
claracruzAuthor Commented:
superb!!!
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now