我有一个字符串:
"1|2 3 4 oh 5 oh oh|e eewrewr|7|".
Run Code Online (Sandbox Code Playgroud)
我想得到第一个管道(|)之间的数字,返回"2 3 4 5".
任何人都可以帮助我使用正则表达式来做到这一点吗?
小智 8
这有用吗?
"1|2 3 4 oh 5 oh oh|e eewrewr|7|".split('|')[1].scan(/\d/)
如果你只想要数字,Arun的答案是完美的.即
"1|2 3 4 oh 5 oh oh|e eewrewr|7|".split('|')[1].scan(/\d/)
# Will return ["2", "3", "4", "5"]
"1|2 3 4 oh 55 oh oh|e eewrewr|7|".split('|')[1].scan(/\d/)
# Will return ["2", "3", "4", "5", "5"]
Run Code Online (Sandbox Code Playgroud)
如果你想要数字,
# Just adding a '+' in the regex:
"1|2 3 4 oh 55 oh oh|e eewrewr|7|".split('|')[1].scan(/\d+/)
# Will return ["2", "3", "4", "55"]
Run Code Online (Sandbox Code Playgroud)