2

In ruby, I often use something like this:

if "my string123" =~ /string(\d+)/
  puts "I got #{$1}"
end

How would I do something similar in javascript? Currently, I've been doing this but it feels dirty.

m = "my string123".match(/string(\d+)/)
if (m)
  puts "I got " + m[1]

Perhaps I should just live with this, but thought I'd ask if there was a syntax subtelty I was missing. Thanks!

6
  • 1
    No, that's correct. The ruby way should feel dirty because it adds a hidden/extra syntax that you must understand. The js way is purely functional. Commented Oct 21, 2010 at 5:12
  • It should be noted that if you want to get only the captured part without the full match then you can use a global match: /regexp/g. Then your match will be returned in m[0]. Commented Oct 21, 2010 at 5:14
  • If this is the first time you use m, it should probably have the var keyword: var m = ...;. Also, I don't think JavaScript has puts. Commented Oct 21, 2010 at 5:14
  • @slebetman - one problem with the /g flag: you'd get "string123", not "123"`, you're forgetting about the group. Commented Oct 21, 2010 at 5:17
  • @Kobi: Eh, yes. I just realised that. I thought the behavior was the opposite of what it is. Commented Oct 21, 2010 at 9:15

2 Answers 2

1

You aren't missing anything.
If m is already defined, you could do if(m = "string".match(/regex/)), but this is less clean anyway, and you cannot use that with var.

Sign up to request clarification or add additional context in comments.

4 Comments

how important is the 'var' anyways? I used to not use it and it seemed to work fine in all browsers? :)
@Kevin - Thanks. As for var, it could be extremely important: stackoverflow.com/search?q=javascript+var
@Kevin You probably know what var, let, and const do at this point, given its been 12 years, but I think I'll tell others if they don't know. (note, var is not often used in modern Javascript). var is a keyword in JavaScript that is used to declare a variable in the current scope. While it is true that in JavaScript, you could declare a variable without using the var keyword, it is generally considered bad practice to do so. Let is another keyword, but prevents you from accessing a variable outside the scope it was defined in (though you can access it in a child scope).
const is another way to declare variables, but you can't redefine a value, nor change it, however, you can mutate the value, for example in this scenario: const myArray = [1,2,3]. This throws an error: myArray = [4,5,6] But this works: myArray.push(4) Or myArray.pop() etc.
0

this is just for testing the appearance of the expression in the text:


    var a = "my string123"; //or given as data..
    var b = /string(\d+)/;
    if (b.test(a)) alert("found some digits inside");

this is for getting an array of matches:


var str = "Watch out for the rock!".match(/r?or?/g);

str then contains ["o","or","ro"]

Comments

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.