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
3
  • And what regexes have you tried so far to solve the problem? Commented May 22, 2018 at 12:09
  • 1
    Possible duplicate of Count number of matches of a regex in Javascript Commented May 22, 2018 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? Commented May 22, 2018 at 12:16

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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 RegExp(/(.*\^.*){3}/); without ' " '
@misanthrop Thanks for improving the regex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.