slash197
slash197

Reputation: 9034

How to get a substring with regex in js

I'm trying to get the content between apostrophes but I messed up something most likely because I only get null.

string.match(/_\('*'\)/)

I need to get OK from this "_('OK')"

Upvotes: 0

Views: 45

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

You did not use a (sub)pattern to match any chars but ' zero/one or more times + you need to capture the contents in between single quotes to later access the Group 1 contents after getting the match with String#match:

var s = "_('OK')";
var res = s.match(/_\('([^']*)'\)/);
if (res) {
  console.log(res[1]);  
}

Upvotes: 1

Related Questions