1

in the following regular expression why are we using $ symbol? what does it mean? can anyone explain? i have been looking for this for a long time?

           function AllowAlphabet(){
           if (!frm.alphabet.value.match(/[a-zA-Z]+$/) && frm.alphabet.value !="")
           {
                frm.alphabet.value="";


                alert("Please Enter only alphabets");
           }
           if(frm.alphabet.value.length > 5)
           alert("max length exceeded");
RunningAdithya
  • 1,358
  • 1
  • 13
  • 21
Ashok kumar
  • 187
  • 3
  • 4
  • 15
  • Then you haven't been looking properly. From the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions): *"$ Matches end of input. If the multiline flag is set to true, also matches immediately before a line break character."*. Also a helpful reference: http://www.regular-expressions.info/. – Felix Kling Sep 05 '13 at 11:58

4 Answers4

3

The dollar sign signifies the end of a line in Regular Expressions.

What it does in this case is make sure that the regex checks that frm.alphabet.value contains a-z or A-Z, immediately followed by the line ending. If you removed the dollar character, "abc1337" would be valid input, although even with the dollar characters, without a caret at the start, "1337abc" will be valid input.

Andreas Eriksson
  • 8,687
  • 7
  • 44
  • 64
2

$ binds the expression to the end of the string (also known as an anchor). The matched string must end in upper or lowercase letters.

Though, FWIW, that expression should also be using the start of string anchor (^) f they want to make sure it's only letters in the string. (Without it a value of 5abc would be passing with that regex).

var foo = '5abc';
if (foo.match(/[a-zA-Z]$/)) {
  // this block is executed despite the 5 being there.
  console.log('match');
}
Brad Christie
  • 96,086
  • 15
  • 143
  • 191
0
  1. Actually it is not jQuery it is Regex(in your case)

  2. $ - If a dollar sign ($) is at the end of the entire regular expression, it matches the end of a line.

Check this regexplained for easy understanding of your regex

Praveen
  • 50,562
  • 29
  • 125
  • 152
0

^ and $ represents the beginnings and endings of a line in regular expressions respectively

Sobin Augustine
  • 3,145
  • 2
  • 22
  • 41