0

I am having issues matching a string using regex in javascript. I am trying to get everything up to the word "at". I am using the following and while it doesn't return any errors, it also doesn't do anything either.

var str = "Team A at Team B";
var matches = str.match(/(.*?)(?=at|$)/);

I tried multiple regex patterns before coming across this SO post, Regex to capture everything before first optional string, but it doesn't to return what I want.

1 Answer 1

1

Remove the ? at your first capturing group, and |$ from your second, and add ^ to mark beginning of string:

str.match(/^(.*)(?=at)/)

Alternatively (I personally find below easier to read, but your call):

str.substr(0, str.search(/\bat\b/))
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.