Ben Muircroft
Ben Muircroft

Reputation: 3034

regex101 Conditional statement is always else

I am using http://www.regexr.com/ and https://regex101.com/ to learn regex

regex101's Quick reference shows

Conditional statement: (?(...)|)

If the given pattern matches, matches the pattern before the vertical bar. Otherwise, matches the pattern after the vertical bar.

I can't get it to work at all

/(^(?!no)if|else)/gm

no if else

yes if else

looks like its broken

/(?:(yes)true|false)/g

yes true false

I need to return a single match using string.match so I stay compatible with a third party. I don't have the option to do anything with the results myself so I won't be able to do multiple regex nor filter results with javascript. what I would like to achive is a regex that asks for

if the sentence starts with the word 'name' or the sentence contains '.classX' then return nothing else return '.classA'

returning eather [""] or [".classA "]

Is this possible at all or am I completly waisting my time?

Upvotes: 3

Views: 588

Answers (2)

Christian Maioli M.
Christian Maioli M.

Reputation: 587

Javascript does not support the full spectrum of regular expression features, which is why your conditionals are not working.

Take a look at the mozilla docs for a complete list of supported features.

Upvotes: 2

szupie
szupie

Reputation: 856

While regex is indeed very powerful, I'd recommend that you delegate the conditionals to javascript.

So for your example at the end of your post, you would just create a pattern to match "name" or ".classX", and if match() returns true, you return "" in javascript. Else, return ".classA".

Upvotes: 0

Related Questions