Tuesday 15 June 2010

Javascript - Regex finding multiple parentheses matches -


Therefore, my code works for input, in which there is a set bracket.

  var again = /^.** ((. *************************************************************************** * Var inPar = userIn.replace (again, '$ 1');   

... which means that when the user enters the chemical formula Cu (NO3) 2, then inside No warning is given in the warning).

However, if Q3 (CO2) 3 is input, only CO2 is being refunded.

I'm not even knowledgeable in regx, why is this happening, and there is such a way that I can put number 3) and CO 2, after they are found in an array?

You want to use instead of String.replace. You also want your regex to match several strings in your corner, so that you can not have ^ (the beginning of the string) and $ (end of the string). And when we match within the brackets, then we can not become greedy, we will use it. *

After stepping through the changes, we receive:

  // Use Match "Cu (NO3) 2 (CO2) 3" .match (/^.*\((.*\)).*$/); ["Q3 (NO3) 2 (CO2) 3", "CO2") /> closes in our match "Q (No. 3) 2 (CO2) 3". Match (/ ^. * \ ((* * * \) * $ /). ["Cu (NO3) 2 (CO2) 3", "CO2"] // Instead of matching the entire string, what we just want is "Cu (NO3) 2 (CO2) 3" .match Search / \ (\) /) (*.); ["(NO3) 2 (CO2)", "No. 3" 2 (CO2 ") // Ohs, we are a bit too greedy, and the same match" Q3 3 " .match (/\((.* ))\)/) ;. ["(NO3)", "NO3"] // As we are searching for only one result, the global flag appears to add "Cu (NO3) 2 (CO2) 3" .match (/ \ (( . *)?) \) / G), ["(NO3)", "(CO2)"] // Global captures whole matches, and ignores our capture groups, lets delete them "Cu (NO3) 2 (CO2) 3" .match (/\(.*?\)/g), ["(NO3)", "(CO2)"] // Now remove brackets to We can use Array.prototype.map for this! Var Element = "Koo (No. 3) 2 (CO2) 3" .mail (/\(.*?\)/ g); Element = element Map (function (return match. Salis (1, -1);}) ["no 3", "CO2"] // and if you want to have the fibroo matte as different as the closing bracket If the elements are to be mentioned then "Q3 (CO3) 3" .match (/\(.*?\)/g); Elements = elements.map (function (match) {return match.substr (1) );}} ["NO3)", "CO2")]    

No comments:

Post a Comment