Javascript 正则表达式帮助

标签 javascript regex

我承认,这么多年过去了,我对正则表达式的理解很烂。我希望有人能尽快帮助我。

var str = "11 FT 0 IN | 10' ( +$2,667.00 )";
var match = str.match(/**no clue what to do**/g);

// results needed
match[0] = "11 FT 0 IN";
match[1] = "10'";
match[2] = "( +$2,667.00 )";

最佳答案

 /^\s*((?:\s*[^\s|])+)\s*\|\s*((?:\s*[^\s(])+)\s*(.+)$/

结果在 matches[1][3] 中。 [0] 始终是整个匹配项。


 ^                 # start of string
 \s*               # initial spaces, if any
 ((?:\s*[^\s|])+)  # non-pipe-or-space characters,
                   #   preceded by some spaces (the "11 FT 0 IN")
 \s*               # more optional spaces
 \|                # the pipe character
 \s*               # even more optional spaces
 ((?:\s*[^\s(])+)  # non-open-parenthesis-or-space characters,
                   #   preceded by some spaces (the "10'")
 \s*               # more or more optional spaces
 (.+)              # just chomp everything beyond (the "( +$2,667.00 )")
 $                 # end of string

关于Javascript 正则表达式帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3172671/

相关文章:

javascript - 动态路由代码不执行任何操作

javascript - Node Js 在生产中不写入日志文件

javascript - 用方括号替换所有内容的正则表达式

javascript - 从 Javascript 到 MySQL 的正则表达式

javascript - 如何删除分隔符和空格或标点符号之间的文本?

php - 替换 MySQL 的 URL 模式

javascript - 如何删除字符串结果中的 [ 和 ]

javascript - 西里尔字母正则表达式+最少 3 个字母+没有数字和空格+末尾不可重复

javascript - 单击按钮添加行

javascript - Firebase Twitter 身份验证和 PhoneGap