0

I have a string from which I need to extract specific texts

let str = 'id = "Test This is" id ="second" abc 123 id ="third-123"';
let res = str.match(/[^id ="\[](.*)[^\]]/g);
console.log(res);

I want the texts in ids only ['Test This is','second','third-123'] But I am getting [ 'Test This is" id ="second" abc 123 id ="third-123"' ] The whole text after first id which I don't want.I need help with the pattern.

2 Answers 2

2

Your pattern uses a negated character class where you exclude matching the listed individual characters, and also exclude matching [ and ] which are not present in the example data.

That way you match the first char T in the string with [^id ="\[] and match the last char ; in the string with [^\]] and the .* captures all in between.

I would suggest using a negated character class to exclude matching the " instead:;

\bid\s*=\s*"([^"]*)"

Regex demo

let str = 'id = "Test This is" id ="second" abc 123 id ="third-123"';
let res = str.matchAll(/\bid\s*=\s*"([^"]*)"/g);
console.log(Array.from(str.matchAll(/\bid\s*=\s*"([^"]*)"/g), m => m[1]));

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

Comments

1

You can simplify this down to a non-greedy regular expression indepedent of where the quotes fall in the string:

let str = 'id = "Test This is" id ="second" abc 123 id ="third-123"';
let res = str.match(/".*?"/g);
console.log(res);

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.