2

I am trying to figure out how to match a pattern in this string:

"Red|1|White|7|Blue|27|Green|56"

As you can see, the numbers can be either 1 or 2 digits long (never anything else).

So, if I had a function like this:

function GetColorNumber(sColorName){
  var sSearchStr="Red|1|White|7|Blue|27|Green|56";
  var sColorNum;
  var sPattern;

  ...

  return sColorNum;
}

How could I use a regular expression in this function return the sColorNum (given an sColorName)?

I've solved this problem using Javascript's indexOf and substring functions but can't seem to figure out the pattern for an answer using regular expressions. The solution should work for all colors, including the first and last ones.

sth
  • 222,467
  • 53
  • 283
  • 367
user815460
  • 1,055
  • 3
  • 11
  • 17
  • 1
    Make your life easy and use a proper data structure :) – Felix Kling Jun 25 '11 at 16:08
  • Why do you want to use regex for this? In situations where string manipulation and regex will both work, it's almost always better to go with string manip. That isn't *always* true, but the pattern you're working with is very simple and static. It's an excellent candidate for string manipulation, as in **sth**'s answer. – Justin Morgan - On strike Jun 25 '11 at 16:12
  • @Justin or my second example which will work in all browsers – mplungjan Jun 25 '11 at 16:22

4 Answers4

1

Change the string to

var colors ={Red:1,White:7,Blue:27,Green:56}

then you can return

colors[sColorName]

Without regEx I would do

var iColorNum = parseInt(sSearchStr.split(sColorName+"|")[1],10)

mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

It would be simpler to just split the string at |:

function GetColorNumber(sColorName){
  var sSearchStr="Red|1|White|7|Blue|27|Green|56";
  var parts = sSearchStr.split('|');
  var key = parts.indexOf(sColorName);
  return parts[key+1];
}
sth
  • 222,467
  • 53
  • 283
  • 367
  • which is JS 1.6 https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf - i.e. not compatible with IE < version 9 – mplungjan Jun 25 '11 at 16:12
1

With a regular expression it could look like this:

function GetColorNumber(sColorName){
  var sSearchStr="Red|1|White|7|Blue|27|Green|56";
  var match = RegExp(sColorName + "\\|(\\d+)").exec(sSearchStr);
  return match[1];
}

Note that this will not work correctly if the user passes in a string that contains characters that have special meaning in regular expressions.

sth
  • 222,467
  • 53
  • 283
  • 367
0

See this post:

var str="Red|1|White|7|Blue|27|Green|56";
var sColorName = "Red"; 
var pat = sColorName + '\\|(\\d{1,2})\\|?';
var re = new RegExp(pat ,"g");
match = re.exec(str);

//match[0] is the full match, match[1] is the color code
document.write(match[1]);

or in a shorter fashion:

function getColor(colorName, str){
    return (new RegExp(colorName + '\\|(\\d{1,2})\\|?' ,"g")).exec(str)[1];
}
Community
  • 1
  • 1
planestepper
  • 3,277
  • 26
  • 38
  • Thanks!! It works perfectly; Can I ask some questions about the regular expression pattern: (1) why are double backslashes needed ("\\" instead of "\") for it to work? (2) I tested the solution and noticed that without the "?", the pattern will not match the last color; why is that? – user815460 Jun 25 '11 at 16:40
  • Double slashes are needed in order to escape the escaping backslash :-). The special character backslash needs to be escaped (by a backslash) to be present on the string. As for the question mark, it means the previous term (in this particular case - it may be used differently) may or may not exist. Since your last color does/may not have an ending pipe ("|"), the expression will match it whether or not it exists. – planestepper Jun 25 '11 at 18:47
  • Thanks very much!! I think the regex solution is the most elegant solution. I wonder if it is more efficient than the solution using the split function proposed by sth. – user815460 Jun 25 '11 at 22:19
  • I can't comment on that as I'm strongly biased. I prefer Regex over direct string manipulation - specially going back and forth from arrays. Again, not sure about performance and cost. – planestepper Jun 25 '11 at 22:23