Iulian Onofrei
Iulian Onofrei

Reputation: 9740

Regex to match string ending with string or nothing

I want to match strings ending with javascript encoded characters (%20, %u200E, etc) or end of line.

I've got this regex: (/\w*?)*(?=(%\w{2,}|[\s/])) which matches /text part in these:

/text
/text
/text%20
/text%u200E

But doesn't matches anything in this: /text (no character at end, not even new line)

Upvotes: 4

Views: 3291

Answers (1)

anubhava
anubhava

Reputation: 785761

Change your regex to:

/(?:\/\w*)*(?=(?:%\w{2,})|[\s/]|$)/;

Since you don't have newline \n at the end of your inputs. Just end of input anchor $ will do the job.

Upvotes: 5

Related Questions