Solved

Jquery Split Regex Then Each To Alert Popup?

Posted on 2014-10-27
5
184 Views
Last Modified: 2014-10-28
Hey, working with Jquery and Regex and trying to load a parsed string into an array. So far the Regex works using Expresso. Problem is the Jquery .Split puts a comma between each character anyway. At least the Jqurey .each displays it that way. Unsure if I have a split problem or array write/read problem?

            var words = "my [cell phone] is so [old] its [not cool] anymore"; // Express regex match return

            var arr1 = words.split(new RegExp('\[(\w*\s*)*\]|\w*\s*|\w*'));

            alert(arr1); //m,y, ,[,c,e,l,l, ,p,h,o,n,e,], ,i,s, ,s,o, ,[,o,l,d,], ,i,t,s, ,[,n,o,t, ,c,o,o,l,], ,a,n,y,m,o,r,e

            $.each(arr1, function (index, value) {
                alert(index + ": " + value);  // opens alert over 50 times
            });
0
Comment
Question by:WorknHardr
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:itnifl
ID: 40407779
Hello. What are you aiming to use this for?
If you access each element in the array and output each element in turn, this will be without the commas. When you access the whole array at once and try to use it as an string, the toString() method is called(except when you use jQuery obviously, see below). This automatically displays the array with each element separated by commas. Don't know if you can override toString() somehow, it wouldn't surprise me. However, it isn't necessary:

Here are some examples - see the link:
http://jsfiddle.net/aL40xbcv/1/

var words = "my [cell phone] is so [old] its [not cool] anymore"; // Express regex match return

var arr1 = words.split(new RegExp('\[(\w*\s*)*\]|\w*\s*|\w*'));

$('#test1').html(arr1.toString());
$('#test2').html(arr1);

$.each(arr1, function (index, value) {
   $('#test3').html($('#test3').html() + " " + index + ": " + value + "<br/>"); 
});

Open in new window

0
 

Author Comment

by:WorknHardr
ID: 40408140
The goal is putting each word into a table using .append(<td><label>'+value+'</td>)
0
 
LVL 2

Accepted Solution

by:
itnifl earned 500 total points
ID: 40408156
OK - don't know if this is exactly what you wanted, but see the fourth try section of the fiddle below for an example:
http://jsfiddle.net/aL40xbcv/2/

Html:
<table id="ourTable" border="1">
    <tr>
    
    </tr>
</table>

Open in new window


jQuery:
$.each(arr1, function (index, value) {
    $('#ourTable tr:first').append("<td><label>" + value + "</td>"); 
});

Open in new window

0
 

Author Comment

by:WorknHardr
ID: 40408382
I have a working regex using Expresso editor, but it doesn't work the same in js.split. I maybe confusing split and match, thought I could do both as same time.  This regex / match (Expresso):  

Regex: \[(\w*\s?)*\]|(\w*\s?)
Input: qq [ww] ee pp uu [kk pp] pp [dd gg jj] mm
Output:
qq
[ww]
ee
pp
uu
[kk pp]
pp
[dd gg jj]
mm
0
 

Author Closing Comment

by:WorknHardr
ID: 40409865
thx
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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)

803 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