DEV Community

[Comment from a deleted post]
Collapse
 
ehaynes99 profile image
Eric Haynes

Just to clarify, regexes exist in every modern programming language, not just javascript. They're often permitted in shell commands and even configuration files for many utilities. While there are some differences between their engines, the vast majority of use cases will have the same syntax across platforms. It's a great skill learn at least at a basic level.

They're even commonly used in IDEs and text editors (E.g. in vscode, in the find/replace dialog, there is a .* button that enables regex). This can get REALLY powerful when you need to make a change to a large number of similar but different values.

For example, let's say you copy/paste a table from a browser, you'll get something like:

first   one
second  two
third   three
Enter fullscreen mode Exit fullscreen mode

If you want to convert it to a block of JSON, you could spend an hour surrounding the keys and values with quotes, and replacing the tab with :. OR, you can use find with:

([^\t]+)\t(.*)
Enter fullscreen mode Exit fullscreen mode

which says: "find a group of stuff that is not a tab (group 1), followed by a tab, followed by the rest of the line (group 2)".

Then replace with:

  "$1": "$2",
Enter fullscreen mode Exit fullscreen mode

which says:

  • two spaces and a quote (")
  • whatever was in group 1
  • quote, colon, space, quote (": ")
  • whatever was in group 2
  • quote, comma (",)

Presto!

  "first": "one",
  "second": "two",
  "third": "three",
Enter fullscreen mode Exit fullscreen mode

Now you have 59 extra minutes. ;)