0

Need to return all string matching a regex.

let text = "data.check && Obj[data.check]['display_119']) {  1549: displayName = obj[data.check]['display_12']";

var identifiers = text.match("display_[0-9]+$");

identifiers should be an array containing

["display_119", "display_12"]; //expected 
3
  • 2
    You can omit the $ like var identifiers = text.match(/display_[0-9]+/g); Commented Aug 23, 2021 at 16:25
  • 2
    You are matching a string, not a regular expression Commented Aug 23, 2021 at 16:25
  • 1
    text.match(/display_[0-9]+/g); Commented Aug 23, 2021 at 16:26

1 Answer 1

2

To get the expected result, you should transform the string you're passing to match() in a regular expression. Also, as commented, you're not expecting results that are only at the end of text, so you don't need $ in your RegExp.

let text = "data.check && Obj[data.check]['display_119']) {  1549: displayName = obj[data.check]['display_12']";

var identifiers = text.match(/display_[0-9]+/g);

console.log(identifiers)

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

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.