正则表达式匹配所有字符到某个标记
16lz
2021-01-22
I'm trying to write a regex to match all characters up to a '/' My current regex only matches the first character of the string
我正在尝试编写一个正则表达式来匹配所有字符到'/'我当前的正则表达式只匹配字符串的第一个字符
var regex = /[^\/]/
regex.exec("something/15")
This will give me 's'. I have also tried
这会给我''。我也试过了
var regex = /.*[^\/]/
but it returns the whole string undelimited by '/'. I have taken a look at this other SO post as well.
但它通过'/'返回整个字符串不受限制。我也看过其他SO帖子。
Javascript regular expression: match anything up until something (if there it exists)
Javascript正则表达式:匹配任何东西直到某些东西(如果它存在)
2 个解决方案
#1
You need to use start anchor and quantifier *
to match 0 or more characters until a /
is found:
您需要使用开始锚点和量词*来匹配0个或多个字符,直到找到/:
var regex = /^[^\/]*/
更多相关文章
- JavaScript中,提取子字符串方法:Slice、Substring、Substr的比较
- java 如何获取动态网页内容,返回字符串
- NodeJS - 解析JSON(只有字符串或数字)
- 如何在JavaScript中对字符串排序
- 将特殊字符转换为HTML实体,Laravel
- JSON.parse(xhr.responseText)意外的字符串错误
- 奇怪的字符显示在XML> ASP.NET> Javascript> HTML中
- 如何在url中将特殊字符作为查询字符串传递
- JS计算任意字符串宽度