1

I am requesting assistance with a regular expression to show that there are at least 3 carets in a string. I've tried using /\^/ but that only finds if the caret exists one time.

Example Data:

KEYWORD^HOSTNAME^MESSAGE^NUMBERS
Cœur
  • 37,241
  • 25
  • 195
  • 267
DWard
  • 119
  • 1
  • 2
  • 8
  • And what regexes have you tried so far to solve the problem? – Luca Kiebel May 22 '18 at 12:09
  • 1
    Possible duplicate of [Count number of matches of a regex in Javascript](https://stackoverflow.com/questions/1072765/count-number-of-matches-of-a-regex-in-javascript) – Andrew Morton May 22 '18 at 12:10
  • /\^/ but that only finds if it exists one time. How can I add that the care must be present 3 times in the string? – DWard May 22 '18 at 12:16

1 Answers1

2

This expression should work:

(.*\^.*){3}

Example in javascript:

var str = "KEYWORD^HOSTNAME^MESSAGE^NUMBERS";
var patt = new RegExp(/(.*\^.*){3}/);
var res = patt.test(str);
console.log(res);
Emeeus
  • 5,072
  • 2
  • 25
  • 37
  • I tried the expression that you provided but am receiving an invalid response. var string = "KEYWORD^HOSTNAME^MESSAGE^NUMBERS"; var re = new RegExp("/.*(.*\^.*){3,}.*/"); if (re.test(string)) { console.log("Valid"); } else { console.log("Invalid"); } – DWard May 22 '18 at 12:27
  • 1
    @Dward RegExp(/(.*\^.*){3}/); without ' " ' – Emeeus May 22 '18 at 12:35
  • 1
    @misanthrop Thanks for improving the regex – Emeeus May 22 '18 at 12:37