in

Magic-RegExp: JavaScript package for regular expressions

5234afb7 shutterstock 214419160

[ad_1]

I like regular expressions (or regex), but part of their longevity is due to being developed separately from mainstream languages. Regular expressions are a way of using pattern matching, and the regular expression language uses standard keyboard symbols as special “meta” characters. This looks strange at first sight.

Writing regular expressions is a bit of a chore — I’ve seen many developers suggest using Copilot to help them out. Now, I don’t use JavaScript as an everyday language, but I’ve talked about regular expressions quite a bit, so it seemed natural to look at magic-regexp, a JavaScript package that uses more English-friendly expressions. .

Now you can pretty much guess why we use packages to express regular expressions.

  • Replacing the pattern with methods and code makes it type safe. The concept of regular expressions is already native to JavaScript.
  • Precompiled methods should be more efficient than live interpretation.
  • Theoretically, it’s easier to read.
  • Regular expression patterns contain meta-rules at the end to guide tricky behavior.

Therefore, you can provide valuable new arrows to your quiver.

There seem to be a myriad of ways to start a JavaScript project, but here’s a quick rundown. I vaguely brought my local environment up to date by running various upgrade dances on the command line.

after that

Let’s go back to our previous article on regular expressions and look at an expression that simply captures the first word of a sentence and its operation on a sample of Shakespearean text.

6162a4a0 untitled

Now, to make sure that the above test captures single-letter words and not mid-sentences, I intentionally mangled Shakespeare (again) and added one line from Ms. Gaynor. to expand. test text:

be8c120c untitled 1

Like that guy in Matrix, I’m happy to read the regex pattern directly, but what if I want to transcribe the above?

So how do we explain that operation on the phone?

“Find first uppercase letter, then any number of lowercase letters. Apply it to entire multi-line text.”

Looking at the usage of magic-regexp it looks like this:

“From the beginning” = at.linestart()

“find uppercase” = letter.uppercase

“any number of lowercase letters” = oneOrMore(letter.lowercase).optionally

“until space” = .and whitespace

So you actually need ‘*’ for zero and above, but you can use oneOrMore and add optionally.

This is testmagicregex.js File containing code:

And this will return the regex on the command line:

and it works:

1f30a9a2 untitled 2

But what’s the extra craft?A pair of question mark colons “?:” non-capturing groupThis means using parentheses to group things, as you would normally do if you wanted to apply a function to everything inside. As it happens, the default usage of parentheses in regular expressions is capture group.

The purpose of the magic-regexp package seems only partially successful. You still have to think in regular expressions. (It reminds me of the 1982 Clint Eastwood movie Firefox, in which the main character has to steal a plane from the USSR, but to operate it he has to think in Russian.)

Shakespeare’s examples are fine, but they are unlikely to be useful in a real JavaScript project. A more practical example is checking for a valid email address.

Again, how do you define valid e-mail schemes on the phone?

“Starts with a name that contains a dot, dash, or underscore as long as it starts and ends with a word letter. Then it must be followed by an at sign (@) followed by a bunch of two characters separated by a colon Stop ringing me now!

We all know there is a specific package to do this, so I don’t want to create my own package for production. But the quick one doesn’t hurt.

So from the conversation you will need at least:

  • oneOrMore(word character)
  • and(anyOf(”.”,”_”,”-”))
  • and(oneOrMore(wordChar))
  • that’s right(“@”)
  • and(oneOrMore(wordChar))
  • and (exactly (”.”))
  • and(oneOrMore(wordChar))

Added some validity tests using claim This time, the above code alone won’t do everything for you, but after a little work, here’s what it looks like:

So the final concern for professional developers is which one is easier to maintain. Is it a straight regex pattern, or is it a representative way of doing the above? I feel like Similar to regex quirks, you have to deal with another package quirk.

However, with a little more development, this could be a solid way to avoid staring at the Matrix.

group Created by sketch.

[ad_2]

Source link

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

    7A8oVfsP Ecu9Bwx8Wt0tI1fxZ1x1Yv492a v iSF4g

    ‘Gruyere’ can be used to describe US cheeses, court rules

    128852816 gettyimages 1471225576

    Arsenal 3-2 Bournemouth: Reiss Nelson gets 97th-minute winner after visitors score inside 10 seconds