-1

At first, I thought the following regex wouldn't match anything, since it's empty:

const emptyText = ''
const regExp = new RegExp(`${emptyText}`)
console.log(regExp) // /(?:)/
const result = 'This shouldn\'t match'.match(regExp)
console.log(result)

But then I realized it will match everything, since everything can be /(?:)/

How to modify this regex (or code), so that an empty text ('') doesn't match everything? And it matches nothing instead?

Current output:

[
  ""
]

Desired output:

null

alexchenco
  • 53,565
  • 76
  • 241
  • 413
  • 2
    By anchoring your expression. Something like `^$` should work. Though why you would not just match against the empty string outside of regexes is kind of a puzzler. :) Technically, the empty string does not match "everything" per se - it matches at every position in the string (i.e the places around characters) since those are interpreted as containing nothing. – oriberu Dec 10 '22 at 17:35
  • 2
    Why would you generate a pattern from an empty variable? Usually the searchstring or whatever should get matched needs to be checked (and also [escaped](https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex)) before being integarted into a pattern. You can `test` it e.g. for at least one word character `\w`. More information would be helpful. Do you match full strings from start to end or match substrings in some text... – bobble bubble Dec 10 '22 at 18:03

2 Answers2

1

You can do something like the following:

const emptyText = '^$'
const regExp = new RegExp(`${emptyText}`)
console.log(regExp) // /(?:)/
const result = 'This shouldn\'t match'.match(regExp)
console.log(result)

Here:

^ matches the beginning of the string and $ matches the end of the string.

Note: the return value is null, means that (null) or "no value at all" is assigned to result.

Update: In case we can't change emptyText, we might concatenate another string inside RegExp constructor like the following:

const emptyText = ''
const rx = '^$';
var regExp = new RegExp(`${emptyText}`)

if(emptyText === '') {
   regExp = new RegExp(`${emptyText}${rx}`)
}
console.log(regExp) // /(?:)/
const result = 'This shouldn\'t match'.match(regExp)
console.log(result)

Here rx is holding the regex value.

samnoon
  • 1,340
  • 2
  • 13
  • 23
  • Thanks. What if I'm in a situation where I can't change `emptyText`? Meaning that it will sometimes be `''` without my control? – alexchenco Dec 10 '22 at 17:45
  • 1
    @alexchenco In that case, we can concatenate the regex value inside another string in the RegExp constructor. Also, then we can add some conditions to check if emptyText is empty or not. Please check my updated code for reference. – samnoon Dec 10 '22 at 18:02
1

You stated the task to get a no match with an empty regex on a string that has 1+ chars. You did not specify what result you expect with an empty regex on an empty string. Assuming you expect null in both cases you can define a regex that never matches for an empty regex string input:

const input = ''; // empty or not, expected to be escaped or valid regex
const regExp = new RegExp(input == '' ? '^(?=$).' : input);
console.log(regExp); // 
let result = 'This shouldn\'t match'.match(regExp);
console.log(result);
result = ''.match(regExp);
console.log(result);

Output:

/^(?=$)./
null
null

Explanation of regex:

  • ^ -- anchor at start of string
  • (?=$) -- positive lookahead for zero chars
  • . -- a single chars
Peter Thoeny
  • 7,379
  • 1
  • 10
  • 20
  • Thanks. I think `input === '' ? '^$' : input` would have the same effect? `^$` isn't matching anything in my app (HTML elements/tags). – alexchenco Dec 11 '22 at 04:49
  • 1
    Yes, depends on your use case, that''s what I meant with "assuming you expect null in both cases" – Peter Thoeny Dec 12 '22 at 19:27