Skip to content Skip to sidebar Skip to footer

Match Pattern Not Working

I'm trying to match strings that only have a, g, c or t in them (insensitive) so the string: 'AAaaatCCCc' is valid while 'catb' is not. this is my function: var pattern = '/^[agct]

Solution 1:

Remove the quotes from the regex literal:

var pattern = "/^[agct]+$/i";

To:

var pattern = /^[agct]+$/i;

There is an implicitly conversion

regexp

A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).

MDN

But it's not working, because you have flags in the pattern and slashes: /../.

var pattern= "^[agct]+$";
... .match(pattern)

Could work due the implicit conversion, but case sensitive because of the missing i flag.

Solution 2:

Assuming your input text is being picked up correctly, a console log of its value might help isolate the issue, this code should work:

if (!this.inputSeq.value.match(/^\s*[agct]+\s*$/i)) {
    this.errMsg = "Invalid input sequence -must contain only a,g,c or t"
    updateErrorBox(this.errMsg);
}

You were using a literal sting instead of a regular expression for the pattern hence the error messages.

Post a Comment for "Match Pattern Not Working"