Hi, everyone, I’m trample man!
Give it a thumbs up!
Preface:
Tencent once had such an interview question: how to give a string of numbers with the decimal system? For example, 9999999999 becomes 9,999,999,999. You might say: Use the method of manipulating string to do, become a string, first divide by 3, see how many pairs, and then add a comma ………… (It’s too much trouble to omit countless steps.) (123456789.123). ToLocaleString (‘ en-us ‘). Yes, but the idea of this article is to explore how to use the re to solve the problem, to become familiar with the re. (Tips:.tolocaleString (‘ en-us ‘) cannot split numbers more than 3 decimal places.)
So, let’s look at some concepts to help solve this problem.
1. Zero-width assertion
writing | The name of the | meaning |
---|---|---|
exp1(? =exp2) | Positive predictive | Find exp1 in front of exp2 |
(? <=exp2)exp1 | Negative predictive | Find exp1 after exp2 |
exp1(? ! exp2) | To look back | Find exp1 not followed by exp2 |
(? <! exp2)exp1 | Negative backward deep | Find exp1 that does not start with exp2 |
Tips: There are many different names for zero-width assertions. All of these mentioned in this article are covered online. The following forward first assertion is forward foresight, which we will use to solve the problem.
To make it easier for you to understand, here’s an example:
- Forward prior assertion (forward zero-width assertion)
'Chinaman'(in the replace (/? = people) /,'rr') Output: RR countrymenCopy the code
- Reverse antecedent assertion
'Chinaman'.replace(/(? <= Chinese) person /,'rr') Output: China RRCopy the code
- Negative forward preemptive assertion (negative zero width assertion)
'Among the Chinese'(in the replace (/? ! / Chinese),'rr') output:"Chinese RR"
Copy the code
- Negative reverse first assertion
'Among the Chinese'(in the replace (/? <! / Chinese),'rr') output:"Rr Chinese"
"abZWab863ab88".replace(/(? <! [A-Z])ab/g,"") output:"ZWab86388"
Copy the code
2. How to solve the problem
This interview question about Tencent:
How to express a string of numbers in thousands? For example, 9999999999 becomes 9,999,999,999. To answer this question, here are a few things to know:
- Replace and the regular convention’s special marker $
- Grouping of grammar
1. The replace() method and the regular convention’s special marker $
writing | meaning |
---|---|
$i | (I :1-99), representing the text matched by the left-to-right regular subexpression |
$& | Represents the full text that matches the regular expression |
$` | ‘: Switch skills key. Represents the text to the left of the matching string |
? | Represents $transfer |
2. Group grammar:
writing | meaning |
---|---|
capture | |
(exp) | Matches exp and captures it to the group automatically named |
(? <name>exp) | Matches exp and captures the text to the group named name |
(? :exp) | Matches exp without capturing the matching text |
The location specified | |
exp1(? =exp2) | Find exp1 in front of exp2 |
(? <=exp2)exp1 | Find exp1 after exp2 |
exp1(? ! exp2) | Find exp1 not followed by exp2 |
(? <! exp2)exp1 | Find exp1 that does not start with exp2 |
annotation | |
(? #comment) | This type of group does not have any effect on regular expression processing, but only provides comments for people to read |
Resolution:
- So what we’re going to do here is we’re going to do
epx1(? =epx2)
(Positive predictive) - Epx1 corresponds to the regular symbol is
\d
.
Because the number before the first comma ranges from 1 to 3, limit the range\d{1-3}
. - Then, exp2 corresponds to the requirement to add one in every three digits
.
soepx2
The canonical expression for is:(\d{3}+$)
(+ : occurs once or more; $: limited to end with XXX) - Finally, the replace() method takes the second argument: use
$&
Symbols.$&,
Said to captureThe full text that matches the regular expression
And replacement for.
- So let me write it this way
'99999999999'. The replace (/ \ d {1, 3} (? =(\d{3})+$)/g, '$&,')
Next: do an extension problem: how to write ‘99999999999.02’ with a decimal point to do thousandth conversion?
Writing a
'99999999999.02'The replace (/ \ d {1, 3} (? =(\d{3})+(? :\.\d+)? $)/g,'$&,) output:"99999999999.02."
'99999999999.33333333'The replace (/ \ d {1, 3} (? =(\d{3})+(? :\.\d+)? $)/g,'$&,) output:"99999999999 to 1, 333333"
Copy the code
Write two
Most of the time there is more than one way to write a regular expression. Another way of writing this expression is:
"123456789.12123".replace(/(? ! (^)? =(\d{3})+(? :\.\d+)? $)/g,', ')
Copy the code
The most important difference between this method and the previous method is that the part of exp1 can not be added to the beginning of the qualified string.
Conclusion: Thank you for reading, if you feel helpful, please give the author a little encouragement, a like or favorites it. If you need to communicate, please contact me on wechat (WX9456D) email ([email protected]).