- read

15 Regular Expression Tricks & Tips Every Developer Should Know

fatfish 180

Photo by Jexo on Unsplash

Preface

What do you think about regular expressions? I guess you’d say it’s too obscure, I’m not interested in it at all. Yes, I used to be like you and thought I would never learn it in my life.

But we can’t deny that it’s really powerful, I use it a lot at work, and I’ve summed up 15 tips to share with you.

If you are interested in how they are implemented, you are very welcome to tell me in the comment area, and I will write another article to analyze it separately.

1. Format money

I often need to format money and it needs to follow these rules:

  1. 123456789 => 123,456,789
  2. 123456789.123 => 123,456,789.123

You can imagine how we would do this without regular expressions?

2. Two ways to implement a trim function

Sometimes we need to remove the leading and trailing spaces of the string, it will be very convenient to use regular expressions, I want to share with you at least two ways.

Way 1

Great, we have removed the leading and trailing spaces of the string ‘string’.

Way 2