Ask coding questions

← Back to all posts
[JS] I need help understanding this code.
MisledWater79 (0)

I need help on specifically /(?<=\$\(CustomRank{Rank-Name: ).+?(?=}\))/g I don't know what it is and how I can edit it to something different but with similar function.

let chatmsg = 'MisledWater79 "$(CustomRank{Rank-Name: CDT})'
console.log(chatmsg.match(/(?<=\$\(CustomRank{Rank-Name: ).+?(?=}\))/g))
Comments
hotnewtop
AlexDaCoder (21)

It is a JavaScript regex (regular expression). Here's the breakdown:

/ // start of regex
  (?<= // start of positive lookbehind (NOT SUPPORTED IN SOME BROWSERS, SO YOUR REGEX MAY BREAK), it checks for stuff preceding the stuff you want to match, and won't count in the match itself, so that part won't get replaced if you are using .replace()
    \$ // a dollar sign char, it needs escape because it's also an metachar meaning [END]
    \( // a ( char, also escaped
    CustomRank{Rank-Name:  // all literal chars, though I would suggest a \ before the { char, since it could also be a metachar
  ) // end of positive lookbehind
  . // any char, it's a wildcard
  + // 1 or more of that char
  ? // lazy: match as least of the char (still the . wildcard) as it will allow
  (?= // start of positive lookahead - same as lookbehind, but supported in all browsers (the common ones, at least) and looks ahead instead of behind
    } // literal } char. again, i suggest a \} instead
    \) // literal ) char
  ) // closing the lookahead
/ // closing the regex
g // global flag, matches for all occasions instead of only 1

Mark if helped!

Coder100 (18915)

That's regex. You can use regexr to parse it to explain it to you.

It will return what is in bold: (**CDT**)

MisledWater79 "$(CustomRank{Rank-Name: **CDT**})

First, it checks if there is a $(CustomRank{Rank-Name:. Then, it will match whatever is inside the curly braces. That's it!

InvisibleOne (3224)

I believe that is regex, but idk what isn't doing