hi chaps, I am having some trouble decoding this regex in a javascript (wasn't entirely sure whether javascript was the right place to post this
since it has more to do with regex). I am very new to it - this is the first time I look into that - and desite having done a bit
of research online on various regex sites I am still not entirely clear how the followoing works:
function numberConversion(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
Basically I know this function change the passed parameter to a string and then inserts a comma after every 3 digits, but I'd like to know what each bit in the regex does.
-I know this \B
makes sure that we don't look at te beginning of the string;
-I for the life of me couldn't determine what this combination does ?=
. I know that the ? if preceded by a character will match that character once or 1 time, but here it seems to be used in combination with the = sign;
-(\d{3})
this is a group and basically says find a group of 3 digits;
- +
: again the + sign from what I know means match the previous character once or more times, but here seems to be more like a kind of concatenation
operator
-(?!\d))
not entirely sure: does it means something like exclude a digit or something?
-g
is a flag, and stands for global match
hope somebody can help me with this. I am not necessarily looking to learn regex but just having a very basic understanding so that I know what's going on
thanks