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 = /^[^\/]*/

更多相关文章

  1. JavaScript中,提取子字符串方法:Slice、Substring、Substr的比较
  2. java 如何获取动态网页内容,返回字符串
  3. NodeJS - 解析JSON(只有字符串或数字)
  4. 如何在JavaScript中对字符串排序
  5. 将特殊字符转换为HTML实体,Laravel
  6. JSON.parse(xhr.responseText)意外的字符串错误
  7. 奇怪的字符显示在XML> ASP.NET> Javascript> HTML中
  8. 如何在url中将特殊字符作为查询字符串传递
  9. JS计算任意字符串宽度

随机推荐

  1. 商品管理 数据库表结构 图片 功能结构
  2. gevent支持Python3
  3. mall数据库表结构概览
  4. 商品模块数据库表解析(一)
  5. Python在终端中写一首七彩的情诗
  6. 商品模块数据库表解析(二)
  7. overloader:在Python中实现重载
  8. 订单模块数据库表解析(一)
  9. 营销模块数据库表解析:首页内容推荐功能
  10. 订单模块数据库表解析(二)