-3

I have the following string:

mmSuggestDeliver(0, new Array("Name", "Category", "Keywords", "Bias", "Extension", "IDs"), new Array(new Array("Advance Auto Parts Inc.", "Aktien", "982516|US00751Y1064|AAP||", "85", "", "Advance_Auto_Parts|982516|1|13715"),new Array("iShares China Large Cap UCITS ETF", "Anzeige", "", "100", "", "http://suggest-suche-A0DK6Z")), 2, 0);

I want to extract the name of the security written in bold.

This is what I try:

var regEx = new RegExp(/"\w+\|/, 'g');
var text = 'mmSuggestDeliver(0, new Array("Name", "Category", "Keywords", "Bias", "Extension", "IDs"), new Array(new Array("Britvic Plc", "Aktien", "A0HMX9|GB00B0N8QD54|||", "85", "", "Britvic|A0HMX9|1|15568"),new Array("<div class=\"pull-left mright-5 image_logo_ishares2\"></div><div class=\"pull-left\">iShares MSCI AC Far East ex-Japan UCITS ETF</div>", "Anzeige", "", "100", "", "http://g.finanzen.net/ishares-suggest-suche-A0HGV9")), 2, 0);';
var securityName = regEx.exec(text);

console.log(securityName);

Only the first match A0HMX9| gets returned. I want the second one. How can I achieve this?

Thank you!

matisetorm
  • 857
  • 8
  • 21
user2145393
  • 499
  • 2
  • 6
  • 11
  • I would suggest you check out lookbehind and lookahead for your expression. Here is some material to get started with: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expression – Thomas Johansen Aug 16 '17 at 09:10
  • Use `var securityNames = text.match(/"\w+\|/g);` – Wiktor Stribiżew Aug 16 '17 at 09:11
  • Why does documentation tell me to add the global flag as second parameter? https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp#grouping-back-references – user2145393 Aug 16 '17 at 09:23

2 Answers2

1

const regex = /"\w+\|/g;
const str = `mmSuggestDeliver(0, new Array("Name", "Category", "Keywords", "Bias", "Extension", "IDs"), new Array(new Array("Advance Auto Parts Inc.", "Aktien", "982516|US00751Y1064|AAP||", "85", "", "Advance_Auto_Parts|982516|1|13715"),new Array("iShares China Large Cap UCITS ETF", "Anzeige", "", "100", "", "http://suggest-suche-A0DK6Z")), 2, 0);`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }

    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}
Nata Khitsan
  • 288
  • 3
  • 12
1

As Wiktor Stribiżew suggested in comments, you probably want String.prototype.match instead of RegExp.prototype.exec:

var regEx = new RegExp(/"\w+\|/, 'g');
var text = 'mmSuggestDeliver(0, new Array("Name", "Category", "Keywords", "Bias", "Extension", "IDs"), new Array(new Array("Britvic Plc", "Aktien", "A0HMX9|GB00B0N8QD54|||", "85", "", "Britvic|A0HMX9|1|15568"),new Array("<div class=\"pull-left mright-5 image_logo_ishares2\"></div><div class=\"pull-left\">iShares MSCI AC Far East ex-Japan UCITS ETF</div>", "Anzeige", "", "100", "", "http://g.finanzen.net/ishares-suggest-suche-A0HGV9")), 2, 0);';
var securityName = text.match(regEx);

console.log(securityName);

RegExp.prototype.exec will return a single match on each execution (see Nata Zakharchuk's answer), whereas String.prototype.match returns all matches (provided that you set the g modifier).

pchaigno
  • 11,313
  • 2
  • 29
  • 54
  • Thank you. It seems my fault was to add the g flag as second argument. I made it like in mozilla doc. Strange this seemed to be wrong? https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp#grouping-back-references – user2145393 Aug 16 '17 at 09:21
  • No, the `g` modifier was fine in your code (I updated my snippet to use the same syntax you used). See my explanation below the snippet. – pchaigno Aug 16 '17 at 09:23
  • Ah, that was the problem. I thought the whole time my regex is wrong ;) – user2145393 Aug 16 '17 at 09:25
  • @user2145393 Do you want me to detail something to accept the answer? – pchaigno Aug 17 '17 at 15:29
  • No, everything fine. I was not able to check your answer as answert without waiting ... and then I forgot it. Sorry! – user2145393 Aug 18 '17 at 06:42