1

How can I modify this script so it checks and makes sure it contains the exact string "abc"?

(/^[a].*/i.test(mystring))

It needs to be case insensitive and take lower and upper caps.

4
  • 1
    As I said in my previous answer, regular-expressions.info is a good resource to learn regular expressions. Use it! Question: Do you want to test whether mystring contains abc or whether it is equal to abc? Commented Mar 11, 2011 at 14:45
  • @Felix, the question asks about the exact string "abc". @detonate, why would you want the overhead and complexity of a regex for a simple text comparison as @Brad mentioned in his answer? Commented Mar 11, 2011 at 15:30
  • @Ken White: But it also says contains. Only the OP can answer this question. Commented Mar 11, 2011 at 15:31
  • I want to make sure it keeps the order correctly and looks for lower and upper caps. Felix, I'm still going through the documentation on Regex. Still need help figuring out these conditions and specific syntax formats. Commented Mar 11, 2011 at 16:58

4 Answers 4

3

Though you can use regex, I feel this is better suited for .toLowerCase & .indexOf:

var hasABC = ("myabcstring".toLowerCase().indexOf("abc") !== -1);

Regex seems like overkill to me.


Answer to a comment:

var mystring = "FoOaBcBaR";
function checkForABC(){
  if (mystring.toLowerCase().indexOf('abc') != -1)
    alert('found ABC!');
  else
    alert('Did NOT find ABC');
}
checkForABC();

Demo

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

3 Comments

Brad, made an edit. It needs to check for uppercase and lowercase.
@detonate: It does, that's why it converts the original string to lowercase, then tests against a lowercase comparer. (Unless I'm misunderstanding, in which cases examples are always nice)
How would I include this in a function using a condition?
1

Just do this:

/^abc$/i.test(mystring);

3 Comments

I think what @detonate means is /abc/i.test(mystring)
Martin, made an edit. It needs to check for uppercase and lowercase.
@detonate: the /i makes it case insensitive.
1

If it's a simple check for a straight string, using something like

var exists = (mystring.toLowerCase().indexOf('abc') > -1);

To avoid excessive Regex usage as they usually command more resources.

Comments

0

think the regex expression your looking for is;

^abc$

Hope that helps.

1 Comment

Desi, made an edit. It needs to check for uppercaps and lowercaps.

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.