Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"reverse" regular expression with JavaScript(node.js)

With regular expression like this /\w/ I can match strings like a, q. Is there any idiomatic way to generate all the strings which match some regex in JS?

Don't think about infinite cases. I just want to describe some sets of possible symbols briefly.

something meaningful instead of

var s = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'

like image 325
kharandziuk Avatar asked Mar 04 '16 15:03

kharandziuk


People also ask

How do you reverse a string in node JS?

First, the string is split into individual array elements using the split() method. str. split("") gives ["h", "e", "l", "l", "o"] . The string elements are reversed using the reverse() method.

How do you clone a regular expression in JavaScript?

We can clone a given regular expression using the constructor RegExp(). Here regExp is the expression to be cloned and flags determine the flags of the clone. There are mainly three types of flags that are used. g:global flag with this flag the search looks for global match.

How do I reverse a string in RegEx?

reverse! # => "This string is backwards." s # => "This string is backwards." To reverse the order of the words in a string, split the string into a list of whitespaceseparated words, then join the list back into a string. The String#split method takes a regular expression to use as a separator.

Can I use RegEx in if statement JavaScript?

The RegEx variable can just then be combined with the test( ) method to check the string. As the result is just a returned boolean (true or false), it can be easily combined with an if/else statement or ternary operator to continue with further actions depending on whether the string is present or not.

How do I reverse a string in JavaScript?

Interviewers may ask you to write different ways to reverse a string, or they may ask you to reverse a string without using in-built methods, or they may even ask you to reverse a string using recursion. There are potentially tens of different ways to do it, excluding the built-in reverse function, as JavaScript does not have one.

Is there a simple regex whose result can be inverted afterwards?

This is a very simple regex whose result can be inverted afterwards. The problem I have is, that I need to do it within the regular expression and not with their result. Something like: In other words: the regular expression should test for a non-existence and return true in that case.

What is the difference between Split () and reversestring () methods in JavaScript?

The split () method splits a String object into an array of string by separating the string into sub strings. The reverse () method reverses an array in place. The first array element becomes the last and the last becomes the first. The join () method joins all elements of an array into a string. function reverseString (str) { // Step 1.

What is the difference between reverse () and join () methods in JavaScript?

The reverse () method reverses an array in place. The first array element becomes the last and the last becomes the first. The join () method joins all elements of an array into a string.


1 Answers

You can try the randexp library:

Randexp will generate a random string that matches a given RegExp Javascript object

See the demo:

document.body.innerHTML = new RandExp(/\w/).gen();
document.body.innerHTML += "<br/>" + new RandExp(/\w/).gen();
document.body.innerHTML += "<br/>" + new RandExp(/[for]{3}/).gen();
document.body.innerHTML += "<br/>" + new RandExp(/I like (cats|dogs|mice)/).gen();
<script src="https://github.com/fent/randexp.js/releases/download/v0.4.1/randexp.min.js"></script>
like image 128
Wiktor Stribiżew Avatar answered Oct 30 '22 19:10

Wiktor Stribiżew