1

In the following code, I have used RegExp to check E with Case Sensitive Modifier i in the string The best things in life are free. But it is returning true even if there is no uppercase E in the string.

<html>
<body>

<script type="text/javascript">
var patt1=new RegExp("E","i");

document.write(patt1.test("The best things in life are free"));
</script>

</body>
</html>
2
  • 6
    The i stands for “case insensitive”. Commented Feb 12, 2012 at 12:14
  • @Gumbo Thats what the problem is. RegExp should return false because there is no uppercase E in the string The best things in life are free Commented Feb 12, 2012 at 12:18

1 Answer 1

5

Regular expressions are case sensitive by default just get rid of the i flag. The i flag stands for ignore case.

note it's typically easier to write regular expressions as:

/E/.test(inputstring)

but if you're just testing for the presence of a fixed string it's better to not use a regex at all

containsChar = (inputstring.indexOf('E') > -1);

You can find out more about regular expressions and indexOf on mozilla's javascript reference site

Sign up to request clarification or add additional context in comments.

Comments

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.