JSFiddle - React, Tailwind, and code Playground

by Spindle

HTML

<script src="https://cdn.jsdelivr.net/gh/eu81273/jsfiddle-console/console.js"></script>

JavaScript

const allowedCharacters = 'abc_123';
const unallowedCharacters = '*&^#';

const regex = /^\w+$/;
const regexString = "/^\w+$/";

// -------

console.log('--- regex without it coming from a string');

const regexObjectWithoutString = new RegExp(regex);

console.log('Should be abc_123: ', allowedCharacters.match(regex));
console.log('should be true: ', regexObjectWithoutString.test(allowedCharacters));
console.log('should be false', regexObjectWithoutString.test(unallowedCharacters));

// all good until now
// -------

console.log('--- this time regex from a string');

const anotherRegexButFromString = new RegExp(regexString);

console.log('Should be abc_123: ', allowedCharacters.match(regexString));
console.log('should be true: ', anotherRegexButFromString.test(allowedCharacters));
console.log('should be false', anotherRegexButFromString.test(unallowedCharacters));