Sunday, 15 July 2012

javascript - Regex: multiline, whitespace only until certain character -



javascript - Regex: multiline, whitespace only until certain character -

i'm using https://www.debuggex.com/ visualize , aid in development of regex strings. node.js target platform i'll executing regex in.

i'm looking find first instance of character, let's { (open brace). character may prefixed 0 or more whitespace characters, such as:

' \t\t \n\n {' ' {' '{'

but may not prefixed non-whitespace, f.ex. these should not match:

'\n\n\t u {' ' r {' '${'

using regex: /\s*{/gmi skips past non-whitespace characters. i've tried limiting these methods:

/[^\s]\s*{/gmi <-- 'none of whitespace characters'..this still matches /(\s{,0})\s*{/gmi <-- doesn't match anything?

i'm using .lastindex property of regex object custom offsets in string. behaviour i'm looking for homecoming null in cases there non-whitespace characters before special character, , homecoming match when there 0 or more whitespace before special.

there multiple ways this, here's one:

/^\s*$/.test(str.slice(0, bracket_pos, str.search(/\{/)))

in english: substring origin of string location of left curly brackets composed solely of 0 or more whitespace characters?

or, split on {:

/^\s*$/.test(str.split('{')[0])

javascript regex

No comments:

Post a Comment