Normally, the search stops after finding the first match.
To ensure you find all matches, use a regex literal enclosed in slashes (e.g., /regex/
) instead of a string enclosed in quotes (e.g., 'string'
).
Additionally, remember to include the /g
flag at the end of your regex pattern. This flag indicates a "global" search, allowing it to scan through the entire string and identify all matches.
let url = "/{id}/{name}/{age}";
let params = url.match(/[^{\}]+(?=})/g);
// ^ do a global search
if(params != null){
params.forEach(param => {
console.log(param);
});
}
For more information, visit MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
The "g" flag in the regular expression options triggers a global search, scanning the entire string for and retrieving all matches.