Jump to content

Number in Text


jimfog
 Share

Recommended Posts

This might look like a silly question but it is in my effort comprehend javascript.Suppose we have a body of text(in a webpage)IF in this body there is a number(5 for example) would this be treated by javascript as a string or as a number?What i want to say is, do special occasions must exist in order for a number in a text be treated as "number" from javascript?I hope i did not confuse you.

Link to comment
Share on other sites

If you can isolate the number from the rest of the text content, it can be treated as a number. A regular expression would be ideal for that, probably. For example:

var s = "dh4dkhjd567kjhkjh890dlgd";var re = /\d+/g;var matches = s.match(re);alert(matches[0]); // "4"alert(matches[1]); // "567"alert(matches[2]); // "890"

The re can get more precise if you need it to.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...