A case study
How to convert a string of integers into thousands of bit separated forms, such as 10000000000, into 10,000,000,000.
Before we knew regular expressions, this was a pretty crazy thing to do, both in terms of code and brain burn, but with regular expressions, it was two or three lines of code! Regular expressions are good at matching and replacing strings that match certain rules.
Regular expression
##### Conceptual regular expressions are also called regular expressions. Regular Expression (often abbreviated to regex, regexp, or RE in code) is a term used in computer science. It’s not just Javascript. Regular expressions can be found in most major operating systems, major development languages, and countless applications. A regular expression is a logical formula used to manipulate strings. It uses predefined characters and the combination of these characters to form a “regular string”. The “regular string” is used to filter strings. # # # # #
- Flexible, logical and functional
- Complex control of strings can be achieved quickly and in a very simple manner.
- For people who are new to it, it’s kind of obscure.
This article only discusses some common and important methods of regular expressions supported by Javascript by solving the above cases. To query the full power of regular expressions in Javascript, please click on Javascript RegExp object here and view the official documentation of W3school.
Thoughts together
To keep in mind, we are going to convert to a number format like this: from the ones digit to the left, we are going to insert a thousand separator in front of every three digits, i.e. we are going to match the empty “” in front of every three digits and replace thousands of separators. The number of digits following each thousands separator is 3 or a multiple of 3.
Code written
Create a regular expression literal with the global matching modifier G. var reg = //g; The W3C’s interpretation of global matching is to look for all matches instead of stopping at the first match.
Because you need to match from right to left, the ending $is required. The three-digit number is represented by \d{3}, and since we don’t know how many sets of such three-digit numbers there are, we need to add + to \d{3} to indicate that any string containing at least one set of three-digit numbers matches. So far, /(\d{3})+$/g denotes three or multiples of three ending digits.
Since we are replacing the “” immediately preceding every three digits (from the end), we need to use forward lookup, i.e.? =n (matches any string immediately followed by the specified string n). How to use forward check? Here’s an example: I have a string var STR = “abaaaaa”; Var reg = /a(?) var reg = /a(?) =b)/g; [” A “], [“a”], [“a”], [“a”], [“a”], [“a”], [“a”], [“a”], [“a”], [“a”] The code for this example is as follows:
var str = "abaaaaa",
reg = /a(? =b)/g;
console.log(str.match(reg));
Copy the code
After a rough understanding of forward prelookup, going back to the original case, we can write it as /(? =(\d{3})+$)/g; Why (? =)… Don’t write anything in front? Because we’re looking for the ones that are followed by three digits. So let’s check. Does that match three “”?
var str = "10000000000",
reg = / (? =(\d{3})+$)/g;
console.log(str.match(reg));
Copy the code
And here it is, three “”.
var str = "10000000000",
reg = / (? =(\d{3})+$)/g;
console.log(str.replace(reg, ","));
Copy the code
The result is as follows, the conversion is successful.
But that’s not all… Now we have eleven digits. If we add a 0 to make twelve digits, it’s a multiple of three. Let’s try it out:
var str = "100000000000",
reg = / (? =(\d{3})+$)/g;
console.log(str.replace(reg, ","));
Copy the code
The result is this:
.
\d
\B
.
var str = "100000000000",
reg = / (? =(\B\d{3})+$)/g;
console.log(str.replace(reg, ","));
Copy the code
conclusion
To sum up, the case of converting a string of integers into thousands of bits is over. Let me just sort out some of the things that we used in this case.
g
Is a modifier that represents a global match that looks for all matches instead of stopping at the first match.$
Is a quantifier indicating the end, e.gn$
, matches any string ending in n.\d
Is a metacharacter to find a number.n{X}
Is a quantifier that matches a string containing a sequence of X n.n+
Is a quantifier that matches any string containing at least one n.? =n
Forward lookup, used to match any string immediately following the specified string n.match()
A String method that finds a match for one or more regular expressions.replace()
A String method that replaces substrings that match the regular expression.\B
Is a metacharacter that matches a non-word boundary and complements the metacharacter\b
To match word boundaries.
This article is not very detailed, but if you are still confused, you should learn regular expressions from the beginning. Comments or suggestions are welcome. Thank you for reading!