1

So I would like to know what's inside the string for example:

var str = "a"; // Letter
var str = "1"; // Number
var str = "["; // Special
var str = "@"; // Special
var str = "+"; // Special

Is there any pre defined javascript function for this? Otherwise I will make it with regex :)

2
  • You might want to check this question: stackoverflow.com/questions/18082/… before delving into Regular Expressions for numeric validation Commented Dec 11, 2010 at 18:29
  • I don't think CIRK is wanting to do actual numeric validation, rather just determine the type of a single character. Certainly, that's the impression I get from the examples in their post. Commented Dec 11, 2010 at 19:06

3 Answers 3

3
if (/^[a-zA-Z]$/.test(str)){
    // letter
} else if (/^[0-9]$/.test(str)){
    // number
} else {
    // other
};

Of course this only matches one character so 'AA' would end up in the //other section.

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

Comments

2

They are all strings...

There isn't anything built in that will do what you want.

A regex may be a good solution, though you have not really provided enough information for one.

1 Comment

@Joel Coehoorn - Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems. ;)
0
if(isNaN(string)){
   //yes is a string
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.