0
var textoNotes = "e|-----------------|B|-----------------|G|-----------------|D|-----------------|A|-----------------|E|3-3-3-3-3-3-3-3-3|"; var filterChords = /(\(|[^(e|B|G|D|A|E)|]+|\))/g; var arr = filterChords.exec(textoNotes); 

This returns:

arr[0] = ----------------- arr[1] = ----------------- 

but, This is what I want:

arr[0] = ----------------- arr[1] = ----------------- arr[2] = ----------------- arr[3] = ----------------- arr[4] = ----------------- arr[5] = 3-3-3-3-3-3-3-3-3 

Thanks you all, and after this I want to get each value in a diferent value.

4
  • 6
    What is the output that you want? Make yourself clearer. Commented Oct 13, 2015 at 13:31
  • Have you tested your regex on regex101.com? It looks fine, if I understood you correctly. Commented Oct 13, 2015 at 13:48
  • yes, nad works in regex but here no. Commented Oct 13, 2015 at 13:49
  • regex101.com/r/cV9oS1/1 Commented Oct 13, 2015 at 13:49

4 Answers 4

1

Note that your regex misbehaves as you tried to use groups inside a character class. In (\(|[^(e|B|G|D|A|E)|]+|\)), you have 3 alternatives:

1) \( matching a literak ( 2) [^(e|B|G|D|A|E)|]+ matching 1 or more characters other than (, e... 3) \) matching a literal ).

You can use the following regex:

[eBGDAE]\|[^|]+\| 

See regex demo

The regex:

  • [eBGDAE] - matches 1 letter from the range
  • \| - matches a literal |
  • [^|]+ - 1 or more characters other than |
  • \| - 1 literal |.

Note that to make sure you match 1 letter in the beginning, you may use a \b word boundary:

\b[eBGDAE]\|[^|]+\| 

JS snippet:

var re = /\b[eBGDAE]\|[^|]+\|/g; var str = 'e|-----------------|B|-----------------|G|-----------------|D|-----------------|A|-----------------|E|3-3-3-3-3-3-3-3-3|'; var res = []; while ((m = re.exec(str)) !== null) { res.push(m[0]); document.getElementById("r").innerHTML += m[0] + "<br/>"; }
<div id="r"/>

The res will contain the array of the values required, and document... is only for demo.

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

Comments

0

Your regular expression doesn't match what you want it seems. I would try using something like below (assuming I am understanding your question correctly, as it's a bit unclear).

text.match(/[e|B|G|D|A|E]\|(.+)\|/g); 

str.match() will output an array of values.

A good place to test your regex is http://www.regexr.com or other similar site.

Comments

0

Why don't you try this:

text.match(/[^\|eBGDAE]+/g); 

Which will just match any stream of characters not on this list including pipes?

Comments

0

So | is one of the special characters that you should escape this which exists outside of [];

<script> var textoNotes = "e|-----------------|B|-----------------|G|-----------------|D|-----------------|A|-----------------|E|3-3-3-3-3-3-3-3-3|"; var filterChords = /[eBGDAE]\|[^eBGDAE|]+\|/g; var arr; while (arr = filterChords.exec(textoNotes)) { alert(arr); } </script> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.