admin管理员组文章数量:1410705
I have url like this domain/news-343dds-this-is-test
I want to extract news id 343dds
,
so I tried to use regex
Here is the regex I used /news-(.+)-/
But the result is like this 343dds-this-is
. I only want get the 343dds
.
I have url like this domain./news-343dds-this-is-test
I want to extract news id 343dds
,
so I tried to use regex
Here is the regex I used /news-(.+)-/
But the result is like this 343dds-this-is
. I only want get the 343dds
.
2 Answers
Reset to default 6The (.+)
is being greedy and matching the rest of the input. Change it to (.+?)
to make it not greedy.
Jeremy Hanlon's solution obviously works but is not the most remended one.
You had better using ([^-]+)
.
Even if it probably doesn't matter in simple cases such as this SO question, the lazy quantifier +?
has the inconvenient that the number of steps is proportional to the size of the searched part, so it may widely impact performance.
This is clearly explained here.
Example:
(.+?)
needs 22 steps for the given343dds
key(.+?)
needs 68 steps for a longer key likethisIsASignificativelyLongKey
([^-]+)
needs 12 steps only for any key
本文标签: javascriptRegex Extract ID From URLStack Overflow
版权声明:本文标题:javascript - Regex Extract ID From URL - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745040977a2639070.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论