0

I need to catch word in some standard string (link id actually), but not to catch all string.

This

(?:color_form_submit_)(\w+)

works in ruby and catches yellow in color_form_submit_yellow

But in JS it seems to catch all string, I can't figure out why.

6
  • 2
    Show your code, what it produces and what you expect instead. Commented Jun 11, 2013 at 12:15
  • 2
    'color_form_submit_yellow'.match(/color_form_submit_(\w+)/)[1] Commented Jun 11, 2013 at 12:18
  • @Jack: and if it doesn't match? Commented Jun 11, 2013 at 12:24
  • @thg435 Then you get a big fat error. Commented Jun 11, 2013 at 12:25
  • @Jack: no, you get an error. My code will work just fine. Commented Jun 11, 2013 at 12:26

4 Answers 4

4

I wanted to achieve it one regex just for elegance

If you want elegance, then you could try something like this

var str = 'color_form_submit_yellow',
    reg = /color_form_submit_(\w+)/;

(str.match(reg) || [])[1]; // "yellow"

and you also have

('fail'.match(reg) || [])[1]; // undefined

This works because

[1, 2, 3] || []; // [1, 2, 3]
null      || []; // []
array[1] === (array)[1];
Sign up to request clarification or add additional context in comments.

1 Comment

@Jack it's undeleted; see this comment ;-)
3

Unfortunately, JS doesn't support lookbehinds and failed match returns null, not an empty array. So the only way to do that in one expression, without if conditions, is something like

color = (str.match(/color_form_submit_(\w+)/) || []).pop()

Example:

> ("color_form_submit_black".match(/color_form_submit_(\w+)/) || []).pop()
"black"
> ("foo bar".match(/color_form_submit_(\w+)/) || []).pop()
undefined

The advantage of this construct compared to the if statement is that it can be used in an expression context, for example, in a function call:

 myRegex = /color_form_submit_(\w+)/
 showColor((myLabel.match(myRegex) || []).pop())

1 Comment

+1, didn't see this. You could also use [1] instead of .pop(), which is what I did in my (now deleted) answer.
1

You need to explicitly look for the match to the first capturing group:

var myregexp = /color_form_submit_(\w+)/;
var match = myregexp.exec(subject);
if (match != null) {
    result = match[1];
}

3 Comments

yeath, I knew I can do that, but I wanted to achieve it one regex just for elegance)
@JoeHalfFace: Sorry, no. Other regex flavors would allow (?<color_form_submit_)\w+, but not JavaScript.
@JoeHalfFace This is only one regex, isn't it?
0
var match = string.match(/color_form_submit_(\w+)/);
match && match[1];

would be the shortest (unnecessarily obfuscated) way you can get in javascript.

7 Comments

sorry, but this is not "the shortest".
@thg435 shortest "readable" version:-p Also performance wise it is very arguable whether calling an array constructor just to pop a non existing value from it just to save one line is a good option. I don't know if you ever worked in a huge project, but your coworkers surely would kill you for this code when just one short line does the same in a far more obvious way.
Can be. Still, calling this solution "the shortest" is incorrect. And it's not about "saving one line". @Paul's and mine solutions can be used in expression contexts, i.e. in a function call, yours - not.
@thg435 edited my answer to give you credit ^_^ But congrats, you nearly won the golf contest;)
@thg435 I only posted my answer because I didn't read yours properly first. It is now deleted, but hey if you're happy for it to re appear I don't mind pressing undelete ^^
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.