Get result String RegEx

Viewed 49

I am trying to get string using RegEx; here is the string:

window.runParams = {};
window.runParams = {blablabla};

How to get the second string {blablabla}? I am using REGEX:

(?<=window.runParams = ").*(?=;)

But that gets the first string {}.

3 Answers
  1. If you want to get string with braces eg: {blablabla}

    window.runParams = ({\w+})

  2. If you want to get only the string inside braces eg: blablabla

    window.runParams = {(\w+)}

Value of group 1 is your result

try modifying your regex so it only accepts matches with non-empty curly brackets \{.+\} such as

(?<=window\.runParams = )(\{.+\})(?=;)

...there's probably ways to simplify the regex further, depending on you problem...my guess is you don't need the lookahead/lookbehind, e.g. in the example given \{.+\} will do just fine (returns {blablabla}) ....but it really depends on the format and content of your file...also remember braces, dots etc have a special meaning in regexes so you probably would want to escape them

The following pattern captures only curly brackets with word character content:

(?<=window.runParams = ){\w+}(?=;)

and will only capture:

{blablabla}

when run against the text:

window.runParams = {};
window.runParams = {blablabla};

See results here:

https://regex101.com/r/mTwA64/1

Related