3

I have a string for ex "adlkup.db.com" and I want to validate the string for ".com" at the end of the string.

var x = "adlkup.db.com";

So I am trying something like

/.com$/.test(x)

and the . is interpreting to some other regex which finds a single character, except newline or line terminator

4
  • 2
    I know you've received the right answer from Josh, but I'm just wondering why are you using regex in the first place. Isn't it easier and clearer to use lastIndexOf(".com") instead? Commented Nov 16, 2015 at 23:58
  • for example "adlkup.db.com".lastIndexOf(".com") would give me 9 and "adlkup.db.com.w3Schools".lastIndexOf(".com") would still give me 9 and I am validating the string should end with ".com". So I assumed that regex would work fine. Commented Nov 17, 2015 at 0:13
  • Yes, but you will be checking it like this: myValue.lastIndexOf(".com") != myValue.length - 5) which will give you false` exactly like the regex does. Commented Nov 17, 2015 at 1:13
  • But there is a corner case where myValue.lastIndexOf(".com") might be -1 and myValue.length - 5 also be -1. So to avoid this I want to use regex. But it would be great if you can give me info on why it's not the better way to use regex for these kind of situations. Thanks in advance. Commented Nov 17, 2015 at 2:19

3 Answers 3

3

A period in a regular expression matches any character.

To make it literal, you need to escape it:

/\.com$/.test('stackoverflow.com'); // true
/\.com$/.test('stackoverflowcom');  // false

Alternatively, as Racil Hilan points out in the comments, you can also use the .lastIndexOf() method in order to check:

var string = 'stackoverflow.com';
string.lastIndexOf('.com') === string.length - 4; // true

or using the .substr() method:

'stackoverflow.com'.substr(-4) === '.com'; // true
Sign up to request clarification or add additional context in comments.

1 Comment

var x = ".com "; /.com$/.test("x") returns true which makes no sense. so how do I validate if the string contains any chars before ".com".
2

In ECMAScript 6 this is done with endsWith:

x.endsWith(".com");

There is a polyfill for old browsers.

1 Comment

This is so cool. Makes my code much cleaner. Thanks for the help.
1

After reading your comments, I think you can use this better than the regex:

var value1 = "adlkup.db.com";
var value2 = "adlkup.db.com.w3Schools";
var value3 = ".com";

document.write(value1 + " " + endWithCom(value1) + "<br/>");
document.write(value2 + " " + endWithCom(value2) + "<br/>");
document.write(value3 + " " + endWithCom(value3) + "<br/>");
               
               
function endWithCom(text){
  if(text.length < 5)
    return false;
  return (text.substr(-4) == ".com");
}

And you can easily convert it to generic function so you can pass it any ending you want to check:

var value1 = "adlkup.db.com";
var value2 = "adlkup.db.com.w3Schools";
var value3 = ".com";
var value4 = "adlkup.db.org";

document.write(value1 + " " + endWithButNotEqual(value1, ".com") + "<br/>");
document.write(value2 + " " + endWithButNotEqual(value2, ".com") + "<br/>");
document.write(value3 + " " + endWithButNotEqual(value3, ".com") + "<br/>");
document.write(value4 + " " + endWithButNotEqual(value4, ".org") + "<br/>");
               
               
function endWithButNotEqual(text, ending){
  if(text.length <= ending.length)
    return false;
  return (text.substr(-ending.length) == ending);
}

5 Comments

I really appreciate for your answer. Previously I tried /\w\.com$/.test(adlkup.db.com) and was wondering why using of regex is not the best way to validate.
I did not say it was not the best way. You definitely can use it. Regex is great for more complex matching, but for simple and fixed matching like yours, it's easy to write a function like you see in my answer. My function will run very slightly faster than the regex. I don't know in what code you will use it, but if you use it in loop on many strings, then the slight difference becomes significant. If you don't have such a scenario, any of the above answers will work for you.
That makes sense to me. Thanks for the help.
The reasons why I only use regex for more complex matching are: 1) regex is much harder to debug. 2) Most programmers are not very good with regex syntax, so the code will be a little harder to maintain. For example if you want to make a more generic function that matches any ending. The function I provided in my answer is clear and all developers can understand it and modify it, but do you know how to do it in regex? It's quite easy, but you need to know how to concatenate regex pattern as a string. 3) regex is often (but not always) a bit slower, but this only important in some scenarios.
In fact I am not so good at regex operations and I wanted to learn small implementations and believe me this is my first attempt to do so. Anyways I am glad that I got more info than what I actually need. Thanks again.

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.