javascript - split strings

I have an input string of the following form:
"2X3,6X1,5X2".

I want to parse this string and extract the number separately.

If possible, please provide some sample code. Thanks!
dshrenikAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
AnuTijiConnect With a Mentor Commented:
Hi

Please find the javascript below for extracting numbers from a string

 function alertNumber() {
        var txt = "2X3,6X1,5X2"
        var numb = txt.match(/\d/g);
        alert(numb);
    }

Open in new window


0
 
KNVB HKSITOCommented:
what is your expected result?
0
 
Gurvinder Pal SinghConnect With a Mentor Commented:
var str = "2X3,6X1,5X2";

var nums = str.split(/[X,]+/);

now num is the array of numbers
0
 
ProculopsisConnect With a Mentor Commented:

...or

"2X3,6X1,5X2".split( /\D/ )
0
 
ZvonkoConnect With a Mentor Systems architectCommented:
Here my script:
<script>
var theInput="2X3,6X1,5X2".split(",");
var theArray = [];
for(var i=0;i<theInput.length;i++){
  if(theInput[i].match(/\s*(\d+)x(\d+)s*/i)){
    theArray.push([RegExp.$1,RegExp.$2]);
  }
}
alert(theArray.join("\n"));
</script>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.