I have created a small Javascript utility to provide this functionality. I don't claim it is perfect, so let me know how you fair. If people like it, I'll make this the official answer to this question.
CharFunk: https://github.com/joelarson4/CharFunk
- CharFunk.getDirectionality(ch) - Used to find the directionality of the character
- CharFunk.isAllLettersOrDigits(string) - Returns true if the string argument is composed of all letters and digits
- CharFunk.isDigit(ch) - Returns true if provided a length 1 string that is a digit
- CharFunk.isLetter(ch) - Returns true if provided a length 1 string that is a letter
- CharFunk.isLetterNumber(ch) - Returns true if provided a length 1 string that is in the Unicode "Nl" category
- CharFunk.isLetterOrDigit(ch) - Returns true if provided a length 1 string that is a letter or a digit
- CharFunk.isLowerCase(ch) - Returns true if provided a length 1 string that is lowercase
- CharFunk.isMirrored(ch) - Returns true if provided a length 1 string that is a mirrored character
- CharFunk.isUpperCase(ch) - Returns true if provided a length 1 string that is uppercase
- CharFunk.isValidFirstForName(ch) - Returns true if provided a length 1 string that is a valid leading character for a JavaScript identifier
- CharFunk.isValidMidForName(ch) - Returns true if provided a length 1 string that is a valid non-leading character for a ECMAScript identifier
- CharFunk.isValidName(string,checkReserved) - Returns true if the string is a valid ECMAScript identifier
- CharFunk.isWhitespace(ch) - Returns true if provided a length 1 string that is a whitespace character
- CharFunk.indexOf(string,callback) - Returns first matching index that returns a true return from the callback
- CharFunk.lastIndexOf(string,callback) - Returns last matching index that returns a true return from the callback
- CharFunk.matchesAll(string,callback) - Returns true if all characters in the provided string result in a true return from the callback
- CharFunk.replaceMatches(string,callback,ch) - Returns a new string with all matched characters replaced
I have created a small Javascript utility to provide this functionality. I don't claim it is perfect, so let me know how you fair. If people like it, I'll make this the official answer to this question.
CharFunk: https://github.com/joelarson4/CharFunk
- CharFunk.getDirectionality(ch) - Used to find the directionality of the character
- CharFunk.isAllLettersOrDigits(string) - Returns true if the string argument is composed of all letters and digits
- CharFunk.isDigit(ch) - Returns true if provided a length 1 string that is a digit
- CharFunk.isLetter(ch) - Returns true if provided a length 1 string that is a letter
- CharFunk.isLetterNumber(ch) - Returns true if provided a length 1 string that is in the Unicode "Nl" category
- CharFunk.isLetterOrDigit(ch) - Returns true if provided a length 1 string that is a letter or a digit
- CharFunk.isLowerCase(ch) - Returns true if provided a length 1 string that is lowercase
- CharFunk.isMirrored(ch) - Returns true if provided a length 1 string that is a mirrored character
- CharFunk.isUpperCase(ch) - Returns true if provided a length 1 string that is uppercase
- CharFunk.isValidFirstForName(ch) - Returns true if provided a length 1 string that is a valid leading character for a JavaScript identifier
- CharFunk.isValidMidForName(ch) - Returns true if provided a length 1 string that is a valid non-leading character for a ECMAScript identifier
- CharFunk.isValidName(string,checkReserved) - Returns true if the string is a valid ECMAScript identifier
- CharFunk.isWhitespace(ch) - Returns true if provided a length 1 string that is a whitespace character
- CharFunk.indexOf(string,callback) - Returns first matching index that returns a true return from the callback
- CharFunk.lastIndexOf(string,callback) - Returns last matching index that returns a true return from the callback
- CharFunk.matchesAll(string,callback) - Returns true if all characters in the provided string result in a true return from the callback
- CharFunk.replaceMatches(string,callback,ch) - Returns a new string with all matched characters replaced
As far as I could tell when faced with a similar problem, the only way was really picking a couple of blocks and assume those are letters. The unicode standard has the full lists, so you could build a complete regex for this (I think). For instance, if you take all characters that are "alphabetic" according to this list you probably have all alphabetic characters. Likewise for numeric (decimal, digit, numeric) in the main unicode data file.
I'm not entirely sure if I'm pointing in the correct direction. There's a bunch of Unicode code charts that might help, and there's of course the unicode standard itself. It's all a bit much to read and understand though, especially if your only goal is to do some javascript string verification.
I don't believe there is a built-in function for that. But it's easy enough to write with a regex
function isLetter(str) {
return str.length === 1 && str.match(/[a-z]/i);
}
With respect to those special characters not being taken into account by simpler checks such as /[a-zA-Z]/.test(c), it can be beneficial to leverage ECMAScript case transformation (toUpperCase). It will take into account non-ASCII Unicode character classes of some foreign alphabets.
function isLetter(c) {
return c.toLowerCase() != c.toUpperCase();
}
NOTE: this solution will work only for most Latin, Greek, Armenian and Cyrillic scripts. It will NOT work for Chinese, Japanese, Arabic, Hebrew and most other scripts.
You could use comparison operators to see if it is in the range of digit characters:
var c = justPrices[i].substr(commapos+2,1);
if (c >= '0' && c <= '9') {
// it is a number
} else {
// it isn't
}
you can either use parseInt and than check with isNaN
or if you want to work directly on your string you can use regexp like this:
function is_numeric(str){
return /^\d+$/.test(str);
}