Solved

regular expressions

Posted on 2008-10-27
11
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

Independent Software Vendors: 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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
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)

707 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