I need to parse in javascript the value entered by the user in an html text field.
That is my first regexp experience.
Here is my code :
var s = 'research library "not available" author:"Bernard Shaw"';
var tableau = s.split(/(?:[^\s"]+|"[^"]*")/);
for (var i=0; i<tableau.length; i++) {
document.write("tableau[" + i + "] = " + tableau[i] + "<BR>");
}
I am expecting to see something like this:
tableau[0] = research
tableau[1] = library
tableau[2] = "not available"
tableau[3] = author:
tableau[4] = "Bernard Shaw"
But instead I got this:
tableau[0] =
tableau[1] =
tableau[2] =
tableau[3] =
tableau[4] =
tableau[5] =
Actually, what I really need is to split this value :
research library "not available" author:"Bernard Shaw"
into this array :
tableau[0] = research
tableau[1] = library
tableau[2] = "not available"
tableau[3] = author:"Bernard Shaw"
But I think there is a problem with positive lookbehind in javascript or something like this.
I did many tries without more success:
- How do I split a string with multiple separators in javascript?
- Regex split string preserving quotes
- Positive look behind in JavaScript regular expression
- javascript split string by space, but ignore space in quotes (notice not to split by the colon too)
I think I really need some help...