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{|}~'
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.
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.
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.
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.
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.
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With