2

Here is the issue:

I go this Code:

var str = {"Acc":10 , "adm_data":"Denied"};

When I do something like:

console.log(str.Acc.match(/[0-9]+/g)) // To Get the Integer Value from the "Acc" key

Firebug Screams:

TypeError: str.Acc.match is not a function

console.log(str.Acc.match(/[0-9]+/g));

See Image:

enter image description here

I always do something like:

var str = "Hello _10";

console.log(str.match(/[0-9]+/g)) // This Works

Why is the Object thingi not working?


PLEASE NOTE:

As mentioned by @Fabrício Matté. The issue was that I was trying to pass an integer Value to the .match method which does not belong to integers. The solution was to do what @kundan Karn Suggested. Something like: str.Acc.toString().match(/[0-9]+/g)// Converting it first to string then match. It worked!

Cœur
  • 37,241
  • 25
  • 195
  • 267
Universal Grasp
  • 1,835
  • 3
  • 20
  • 29
  • [SSCCE](http://sscce.org/) or didn't happen. Impossible to reproduce with your given example. http://jsfiddle.net/QgdUV/ – Fabrício Matté Oct 31 '13 at 08:08
  • Tried your code and it works fine :) I used nodeJS to test it though – Rami Oct 31 '13 at 08:11
  • @Fabricio Matte ... Can you Eplain what You mean?.. @surrender's suggestion worked well but when I try Something like: `var str = {"Acc":10,"adm_data":"Denied"};` _Notice the **Raw Integer** value... This gives the Same error back why? – Universal Grasp Oct 31 '13 at 08:18
  • @UniversalGrasp What I mean is that it is impossible to reproduce the issue with the code in the question. Now that you've shown a number value, the error happens because Numbers do not have inherit `match` which is a string method. Kundan and Kristof's answers should solve the issue. – Fabrício Matté Oct 31 '13 at 08:21
  • 1
    **To All [DownVoters] of this Question**: Please Stop downVoting this question... You may be _Experts_ and _GURUs_ but there are guys like me who might face the same issue and this [Question] might be useful... Thx – Universal Grasp Oct 31 '13 at 08:24
  • @FabrícioMatté... Instead of downvoting... You'd have Suggestd an Edit to the [Question] – Universal Grasp Oct 31 '13 at 08:25
  • 1
    @UniversalGrasp I'd have suggested an edit, but at the time of asking there was no way to determine the actual issue. I've removed the DV as I see that you've edited the question. – Fabrício Matté Oct 31 '13 at 08:46
  • @FabrícioMatté Indeed thanks immensely for you quick scrutiny... That indeed helped me Edit... Lots of Respect to You!..NOTE: We are in the Same Office with UniversalGrasp.. I controll Social Networking Flows – ErickBest Oct 31 '13 at 08:49
  • @Universal Grasp.. Welcome buddy. You should thank _FabrícioMatté_ more – ErickBest Oct 31 '13 at 08:54

2 Answers2

4

match function works with string. So convert it to string first

str.Acc.toString().match(/[0-9]+/g)
kundan Karn
  • 226
  • 1
  • 8
  • PERFECT!... Exactly it... To All [DownVoters] of this Question: Please Stop downVoting this question... You may be Experts but there are guys like me who might face the same issue and this [Question] might be useful... – Universal Grasp Oct 31 '13 at 08:23
1

It works just fine: http://jsfiddle.net/nKHLy/

but in order to get rid of the error you might want to try:

var str = {"Acc":"Hello_10" , "adm_data":"Denied"};
console.log(String(str.Acc).match(/[0-9]+/g));

or

var str = {"Acc":"Hello_10" , "adm_data":"Denied"};
console.log(str.Acc.toString().match(/[0-9]+/g));

To know the difference between the 2 options, check: What's the difference between String(value) vs value.toString()

Community
  • 1
  • 1
Kristof Feys
  • 1,822
  • 1
  • 19
  • 36