js apply regex to string code example
Example 1: javascript escape regex
function escapeRegExp(input) {
const source = typeof input === 'string' || input instanceof String ? input : '';
return source.replace(/[-[/\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}
Example 2: js string to regex
const regex = new RegExp('https:\\/\\/\\w*\\.\\w*.*', 'g');
Example 3: js match any number string
const match = 'some/path/123'.match(/\/(\d+)/)
const id = match[1] // '123'