A general requirement many people encounter while working with javascript strings is to check if the first letter of the string is a number. This article demonstrates easy ways to check if the string starts with a digit using different methods and illustration examples.
Table of Contents:
- Check if String Starts with Number using RegExp and test()
- Check if String Starts with Number using RegExp and match()
- Check if String Starts with Number using ASCII Code
Check If String Starts with Number using RegEXP and test()
JavasScript’s test() method will execute a search for a match between the regular expression and the specified string. This method takes an argument that is a string against which the regular expression is matched. If the match is found, true is returned. Else, false is returned.
Syntax: test(string)
Example:-
Check if the below strings start with number
- “1234javaScript”
- “JAVASCRIPT”
Code:-
Frequently Asked:
- Javascript: Get min element of an array
- Get Unique Values from an Array of Objects
- Javascript: Check if string contains only digits
- Javascript: String remove until a character
function checkFirstLetterNumber(_string) { return /^\d/.test( _string); } let dummyString1 = "1234javaScript"; let dummyString2 = "JAVASCRIPT"; //usage of the function console.log( checkFirstLetterNumber(dummyString1) ); console.log( checkFirstLetterNumber(dummyString2) );
Output:-
true false
Explanation:-
- Here in the above code test() method is used to match the pattern against the _string passed as an argument, the regular expression is /^\d/.
- / and / specifies the start and end of the expression.
- ^ specifies the beginning of the string
- \d determines that only digits match the pattern.
- If the expression is matched, then return true; otherwise, false is returned.
Check If String Starts with Number using RegEXP and match()
JavaScript’s match() method will return the result of a string matching against a regular expression. This method takes a regular expression object as a parameter and returns an array of matching results.
Syntax: match(regExp)
Example:-
Check if the below strings start with number
- “1234javaScript”
- “JAVASCRIPT”
Code:-
function checkFirstLetterNumber(_string) { return _string.match(new RegExp(/^\d/)) !== null; } let dummyString1 = "1234javaScript"; let dummyString2 = "JAVASCRIPT"; console.log( checkFirstLetterNumber(dummyString1) ); console.log( checkFirstLetterNumber(dummyString2) );
Output:-
true false
Explanation:-
- The method match() is called here in the above code by the calling string. The regular expression /^\d/ is passed as an argument to the match() method .
- / and / specify the start and end of expression.
- ^ specifies the beginning character.
- \d determines that the character is a digit.
- If the first character is digit, then the matching expression is returned. Else, null is returned.
- Hence, if something is retrieved, the first character is a digit. Otherwise, it is not.
Check If String Starts with Number using ASCII Code
ASCII codes are the numeric value given to the characters and symbols for storing and manipulation by computers.
JavaScript’s charCodeAt() method will return an integer ranging from 0 and 65535 that depicts the UTF-16 code value at the given index.
Example:-
Check if the below strings start with number
- “1234javaScript”
- “JAVASCRIPT”
Code:-
function checkFirstLetterNumber(_string) { return _string.charCodeAt(0) >= 48 && _string.charCodeAt(0) <= 57; } let dummyString1 = "1234javaScript"; let dummyString2 = "JAVASCRIPT"; console.log( checkFirstLetterNumber(dummyString1) ); console.log( checkFirstLetterNumber(dummyString2) );
Output:-
true false
Explanation:-
Here in the above code, we check if the character’s ASCII code placed at 0th index falls between 48 and 57, where 48 and 57 are included. This check is made using the method charCodeAt(0).
The digits 0-9 ASCII code range is: 48 to 57.
I hope this article helped you check if the first letter is number in a javascript string. Good Luck !!!