简体   繁体   English

如何在JavaScript中使用正则表达式匹配第n个字符后的所有字符?

[英]How to match all characters after nth character with regex in JavaScript?

I want to match all characters after 8th character. 我希望在第8个字符后匹配所有字符。 And not include first 8! 并不包括前8!

I need exactly a regular expression cause a framework (Ace.js) requires a regexp, not a string. 我需要一个正则表达式,因为框架(Ace.js)需要regexp,而不是字符串。 So, this is not an option: 所以,这不是一个选择:

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. 更新:我不能调用replace()substring()等因为我没有字符串。 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. 正如我上面已经说过的那样,框架(Ace.js)要求我提供正则表达式。

(?<=^.{8}).* 

will match everything after the 7th position. 将匹配第7个位置后的所有内容。 Matches 89 in 0123456789 匹配89 0123456789

or IJKLM in ABCDEFGHIJKLM ABJEFGHIJKLM的IJKLM

etc 等等

console.log("123456789".match(/^.{8}(.*)/)[1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM