Solved

javascript - split strings

Posted on 2011-09-05
5
223 Views
Last Modified: 2012-05-12
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
Comment
Question by:dshrenik
5 Comments
 
LVL 7

Expert Comment

by:cstsang
ID: 36486716
what is your expected result?
0
 
LVL 4

Accepted Solution

by:
AnuTiji earned 125 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);
        alert(numb);
    }

Open in new window


0
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 125 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

by:Proculopsis
Proculopsis earned 125 total points
ID: 36486791

...or

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

Assisted Solution

by:Zvonko
Zvonko earned 125 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]);
  }
}
alert(theArray.join("\n"));
</script>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I   ajax html table  rows? 20 63
Angular 2 - Issue in page Layout 2 48
Javascript Hashmap output 8 46
is this a cms? 8 35
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now