Solved

Jquery Split Regex Then Each To Alert Popup?

Posted on 2014-10-27
5
181 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

708 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

18 Experts available now in Live!

Get 1:1 Help Now