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
  • Learn & ask questions
Solved

Jquery Split Regex Then Each To Alert Popup?

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
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…

839 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