javascript - split strings

dshrenik
dshrenik used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
what is your expected result?
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


var str = "2X3,6X1,5X2";

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

now num is the array of numbers

...or

"2X3,6X1,5X2".split( /\D/ )
Zvonko זְאֵבSystems architect
Top Expert 2006
Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial