I am doing some form validation and one of my aims is to make a big regular expression with all kinds of inappropriate words. This will be quite long and therefore I don't want to write it several times in different functions that check different parts of different forms such as titles, descriptions etc. Therefore, I was wondering how could I make a regular expression in one file and then include it in my JavaScript files? Or maybe there is a better way to do it?
right now one of my JavaScript functions has this:
var pattern = new RegExp(/\b(badword1|badword2)\b/i);
but of course I need it to be long, I'll be using the "Full List of Bad Words and Swear Words Banned by Google"
You have to use ES6 modules (import / export
) or Common.js modules (module.export / require
) to import javascript code from external files. for example:
// in your Regex file:
let pattern = new RegExp(/\b(badword1|badword2)\b/i);
export pattern
// in your main file
import patten from 'path/to/your/regex/file'
You can read this article for more information about ES6 module importing/exporting.