Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to match all characters after 8th character. And not include first 8!

I need exactly a regular expression cause a framework (Ace.js) requires a regexp, not a string. So, this is not an option:

var substring = "123456789".substr(5);

Can I match everything after nth character using regex in JavaScript?

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.

like image 671
Aleksei Chepovoi Avatar asked Dec 09 '13 16:12

Aleksei Chepovoi


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

How do you match a sequence in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .

What is full match in regex?

The fullmatch() function returns a Match object if the whole string matches the search pattern of a regular expression, or None otherwise. The syntax of the fullmatch() function is as follows: re.fullmatch(pattern, string, flags=0)

What does the plus character [+] do in regex?

A regular expression followed by an asterisk ( * ) matches zero or more occurrences of the regular expression. If there is any choice, the first matching string in a line is used. A regular expression followed by a plus sign ( + ) matches one or more occurrences of the one-character regular expression.


2 Answers

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

will match everything after the 7th position. Matches 89 in 0123456789

or IJKLM in ABCDEFGHIJKLM

etc

like image 73
David Berndt Avatar answered Oct 19 '22 00:10

David Berndt


console.log("123456789".match(/^.{8}(.*)/)[1])
like image 5
Niko Sams Avatar answered Oct 19 '22 01:10

Niko Sams