0

If we have a string, say

'1, 2, 3, 4'
'1, 2, 3 GO!'
'ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!'

then how do I recognize that its has numbers and return true?

I need it because while using,

if(input == input.toUpperCase())

if a number is entered this condition returns true, so need to add an elegant way to stop the numbers from passing this condition.

Edit1: The string is indeed comma separated, I already have googled ways that recognize non comma separated strings as numbers.

Edit2: Added more examples of what I need to pass.

  • 1
    `alert('(1, 2, 3, 4)'.search(/\d/) !== -1);` – Cheery Oct 05 '14 at 18:39
  • Does regex meet your requirements? What other strings do you need to be able to parse? – Brad Oct 05 '14 at 18:39
  • @Brad Regex is my last resort, all alphabets, special characters and umlauts. – Prakash Wadhwani Oct 05 '14 at 18:42
  • 1
    @PrakashWadhwani Why would regex be your last resort if it does exactly what you want? And, I'm saying that if you want to parse a wide variety of strings, you should provide more than one example in your question. Otherwise, it isn't clear what you're trying to do. – Brad Oct 05 '14 at 18:43
  • @Brad its classical problem called Bob, you can read about it here, http://jsfiddle.net/pzoc03vm/ and the tests that need to be passed are, http://jsfiddle.net/7yzfpor9/ – Prakash Wadhwani Oct 05 '14 at 18:45

1 Answers1

1

Regular expressions are appropriate for this:

var str = "(1, 2, 3, 4)";
var containsNumber = /\d/.test(str);      // true

Here, \d is a regular expression that will match if your string contains a digit anywhere.


Ok, looks like you need to look for lowercase letters... If there's any lowercase letter it shouldn't be considered yelling.

var str = "(1, 2, 3, 4)";
var containsLowercaseLetters = /[a-z]/.test(str);      // false

This will only work for latin letters though. In your case, it may be simpler to just state:

var isYelling = (input == input.toUpperCase()) && (input != input.toLowerCase());
Community
  • 1
  • 1
Lucas Trzesniewski
  • 47,154
  • 9
  • 90
  • 138