Home > OS >  How to get specific text from a string in regex
How to get specific text from a string in regex

Time:11-19

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.

CodePudding user response:

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]));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related