Zack Mubarak
Zack Mubarak

Reputation: 21

Get result String RegEx

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 {}.

Upvotes: 2

Views: 69

Answers (3)

mrxra
mrxra

Reputation: 862

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

Upvotes: 0

Gustav Rasmussen
Gustav Rasmussen

Reputation: 3961

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

Upvotes: 0

jnrdn0011
jnrdn0011

Reputation: 417

  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

Upvotes: 1

Related Questions