Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining regular expressions

How to join several (javascript) regular expressions into a single one?

For example, given [/^abcd$/,/^abxy$/,/^abz$/] the output will be /^ab(cd|xy|z)$/.

Is it even computationally possible?

like image 307
user1088045 Avatar asked May 06 '13 14:05

user1088045


People also ask

How do you add regular expressions together?

to combine two expressions or more, put every expression in brackets, and use: *?

What does * represent in regex?

*$ means - match, from beginning to end, any character that appears zero or more times. Basically, that means - match everything from start to end of the string. This regex pattern is not very useful. Let's take a regex pattern that may be a bit useful.

Is there a regex and operator?

In Chain Builder, you can use regular expression (regex) operators to match characters in text strings, such as to define patterns for: Mapping transformation rules for a Data Prep connector pipeline. The File Utilities connector's Find, Find and replace, and Split file commands.

What are flags in regex?

A regular expression consists of a pattern and optional flags: g , i , m , u , s , y . Without flags and special symbols (that we'll study later), the search by a regexp is the same as a substring search. The method str. match(regexp) looks for matches: all of them if there's g flag, otherwise, only the first one.


1 Answers

It is quite easy to make such a tool for simple cases. Just put each pattern into parentheses and join them with "|". So for your example set of patterns it becomes:

/(^abcd$)|(^abxy$)|(^abz$)/

On a second thought, parentheses might not be necessary, so this will do:

/^abcd$|^abxy$|^abz$/
like image 162
spbnick Avatar answered Oct 13 '22 16:10

spbnick