Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

javascript - split strings

Posted on 2011-09-05
Medium Priority
238 Views
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!
0
Question by:dshrenik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 7

Expert Comment

ID: 36486716
0

LVL 4

Accepted Solution

AnuTiji earned 500 total points
ID: 36486730
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);
}
``````

0

LVL 40

Assisted Solution

Gurvinder Pal Singh earned 500 total points
ID: 36486745
var str = "2X3,6X1,5X2";

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

now num is the array of numbers
0

LVL 20

Assisted Solution

Proculopsis earned 500 total points
ID: 36486791

...or

"2X3,6X1,5X2".split( /\D/ )
0

LVL 63

Assisted Solution

Zvonko earned 500 total points
ID: 36486863
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]);
}
}
</script>
``````
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month8 days, 21 hours left to enroll