Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You want to use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match">String.match</a> instead of String.replace. You'll also want your regex to match multiple strings in parentheses, so you can't have ^ (start of string) and $ (end of string). And we can't be greedy when matching inside the parentheses, so we'll use .*?</p> <p>Stepping through the changes, we get:</p> <pre><code>// Use Match "Cu(NO3)2(CO2)3".match(/^.*\((.*\)).*$/); ["Cu(NO3)2(CO2)3", "CO2)"] // Lets stop including the ) in our match "Cu(NO3)2(CO2)3".match(/^.*\((.*)\).*$/); ["Cu(NO3)2(CO2)3", "CO2"] // Instead of matching the entire string, lets search for just what we want "Cu(NO3)2(CO2)3".match(/\((.*)\)/); ["(NO3)2(CO2)", "NO3)2(CO2"] // Oops, we're being a bit too greedy, and capturing everything in a single match "Cu(NO3)2(CO2)3".match(/\((.*?)\)/); ["(NO3)", "NO3"] // Looks like we're only searching for a single result. Lets add the Global flag "Cu(NO3)2(CO2)3".match(/\((.*?)\)/g); ["(NO3)", "(CO2)"] // Global captures the entire match, and ignore our capture groups, so lets remove them "Cu(NO3)2(CO2)3".match(/\(.*?\)/g); ["(NO3)", "(CO2)"] // Now to remove the parentheses. We can use Array.prototype.map for that! var elements = "Cu(NO3)2(CO2)3".match(/\(.*?\)/g); elements = elements.map(function(match) { return match.slice(1, -1); }) ["NO3", "CO2"] // And if you want the closing parenthesis as Fabrício Matté mentioned var elements = "Cu(NO3)2(CO2)3".match(/\(.*?\)/g); elements = elements.map(function(match) { return match.substr(1); }) ["NO3)", "CO2)"] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload