+ 1

[JavaScript] Matching anything in between colons?

So I need to match and return any value that's in between 2 colons. So "n3rfb3fib:eh:rjkbib" would return "eh". How can I do this?

12th Aug 2018, 8:51 PM
Daniel Cooper
Daniel Cooper - avatar
4 Réponses
+ 6
var str = "n3rfb3fib:eh:rjkbib"; alert( str.match(/:(.*):/)[1] ); // eh
13th Aug 2018, 3:54 AM
Kirk Schafer
Kirk Schafer - avatar
+ 2
var match = str.match(/[;|:].*?[;|:]/);
13th Aug 2018, 1:29 AM
Calviղ
Calviղ - avatar
0
is it important to have a opening and closing colon?
12th Aug 2018, 8:59 PM
Roel
Roel - avatar
0
If you're asking if it matters whether or not to match the colons themselves, no, it's not important. If you're asking if the value needs to be between 2 colons, yes, it's important. (Only read further if you want to know the exact issue) I'll tell you why I need this if it helps. Take this error for example: ReferenceError: i is not defined at Events (<anonymous>:5:55) I need to match the 5 (Aka the line number) and display it. The line numbers seem to be between colons, so i'm trying to match that and display it. The error I want displayed should be something like this: ReferenceError: i is not defined (Line 5)
12th Aug 2018, 9:12 PM
Daniel Cooper
Daniel Cooper - avatar