0

I'm wanting to use javascript regexp to test if my string matches a regular expression. However, the regular expression that I'm trying to use causes my javascript to break.

I understand that I can typically do the following:

/e/.test("blah")

but when I try to use my expression, my browser barfs:

/(?<=^| )\d+(\.\d+)?(?=$| )|(?<=^| )\.\d+(?=$| )/.test("1.23")

What am I missing?

2
  • 1
    Javascript does not support look-behind assertions. Commented Oct 2, 2014 at 22:46
  • 1
    Why is this tagged R? Commented Oct 2, 2014 at 22:47

1 Answer 1

2

Javascript does not support Negative or Positive Lookbehind assertions. Therefore, your browser should be throwing an error for Invalid regular expression: ( Invalid group ) because of the Positive Lookbehind (?<=^| ) ...

I would start off with using beginning of string ^ and end of string $ anchors ..

/^\d+(?:\.\d+)?$/.test("1.23")

And if you really want to assert the beginning of string/whitespace or end of string/whitespace positions, you could compile them using capturing groups instead of lookaround assertions since you are only using the test method.

/(^| ) PATTERN ($| )/
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.