Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating strings from regular expression in JavaScript

I think it can be done by generating strings using brute force and then try to match them to the supplied regex and printing if match.

But is there a better way to do this?

Regex are used to test if a string matches a pattern. I am aware of that. I thought it would be interesting to do it the way around.

like image 879
rahulroy9202 Avatar asked Feb 04 '14 14:02

rahulroy9202


People also ask

How do you turn a regular expression into a string?

Well you need to escape it, creating the regular expression \\ . To create that regular expression, you need to use a string, which also needs to escape \ . That means to match a literal \ you need to write "\\\\" — you need four backslashes to match one!

What are the string methods available in regular expression in JavaScript?

In JavaScript, regular expressions are often used with the two string methods: search() and replace() . The search() method uses an expression to search for a match, and returns the position of the match. The replace() method returns a modified string where the pattern is replaced.

Which string methods are used with regular expression?

Regular expressions are used with the RegExp methods test() and exec() and with the String methods match() , replace() , search() , and split() .

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.


1 Answers

If you're using JavaScript, there's Randexp which generates random strings that match a given regex.

Releases for browser

like image 155
Philipp Gayret Avatar answered Oct 07 '22 19:10

Philipp Gayret