如何在 JavaScript 中用正则表达式匹配第 n 个字符之后的所有字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20475706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to match all characters after nth character with regex in JavaScript?
提问by Aleksei Chepovoi
I want to match all characters after 8th character. And not include first 8!
我想匹配第 8 个字符之后的所有字符。并且不包括前8个!
I need exactly a regular expression cause a framework (Ace.js) requires a regexp, not a string. So, this is not an option:
我需要一个正则表达式,因为框架 (Ace.js) 需要一个正则表达式,而不是字符串。所以,这不是一个选择:
var substring = "123456789".substr(5);
Can I match everything after nth character using regex in JavaScript?
我可以在 JavaScript 中使用正则表达式匹配第 n 个字符之后的所有内容吗?
Updates:I can't call replace()
, substring()
etc because I don't have a string. The string is known at run time and I don't have access to it. As I already said above the framework (Ace.js) asks me for a regex.
更新:我不能叫replace()
,substring()
等等,因为我没有一个字符串。该字符串在运行时是已知的,我无权访问它。正如我上面已经说过的,框架 (Ace.js) 要求我提供正则表达式。
回答by David Berndt
(?<=^.{8}).*
will match everything after the 7th position. Matches 89in 0123456789
将匹配第 7 个位置之后的所有内容。在 0123456789匹配89
or IJKLMin ABCDEFGHIJKLM
或ABCDEFGHIJKLM 中的IJKLM
etc
等等
回答by Niko Sams
console.log("123456789".match(/^.{8}(.*)/)[1])