0

Am trying to extract all word character from a url to remove the leading "/"

"/assedede".match(/\w*/) // "" empty string

The above code do not match any character. but

"/assedede".match(/\w+/) // "assedede"

why is /\w*/ not working?

Pac0
  • 21,465
  • 8
  • 65
  • 74
Evergreen
  • 1
  • 2
  • `\w*` matches an empty string before `/` because `*` can match 0 or more occurrences of the quantified pattern, and `str.match(/.../)` only searches for the first match. – Wiktor Stribiżew Feb 28 '20 at 08:18
  • it is working. But since it match an empty string, then it matches the empty string before the slash. Then, it tries to gather more, but since the first character being checked is a slash, it stops here, happy to have matched an empty string. – Pac0 Feb 28 '20 at 08:18
  • On the contrary, the `\w+` begins its match at the `a`, then greedily take all the letters that follow – Pac0 Feb 28 '20 at 08:19
  • PS : I fixed the question, the match for the first is _not_ `null`,it's empty string. – Pac0 Feb 28 '20 at 08:20
  • Yeah, see https://stackoverflow.com/questions/30360439/why-does-string-match-d-return-an-empty-string – Wiktor Stribiżew Feb 28 '20 at 08:30

0 Answers0