npm.io
5.2.0 • Published yesterday

@putout/operator-regexp

Licence
MIT
Version
5.2.0
Deps
2
Size
6 kB
Vulns
0
Weekly
0

@putout/operator-regexp NPM version

Putout operator adds ability to determine that provided RegExp can be converted to String without losing it's sense.

It is used for example in regexp/convert-replace-to-relace-all:

-'hello'.replace(/hello/g, 'world');
+'hello'.replaceAll('hello', 'world');

Install

npm i putout @putout/operator-regexp

API

isSimpleRegexp(regexp: RegExp)
const {operator} = require('putout');
const {isSimpleRegExp} = operator;

isSimpleRegExp(/hello world/);
// returns
true;

isSimpleRegExp(/^hello/);
// returns
false;
transformRegExp(regexp: String, plugin: Traverser)
transformRegExp('[aab]', {
    report,
    fix,
    traverse,
});
getStringFromRegExp(pattern: String)

To avoid errors like this:

SyntaxError: Expecting Unicode escape sequence \uXXXX

const string = getStringFromRegExp(regexpNode);

License

MIT

Keywords