Solved

Jquery Split Regex Then Each To Alert Popup?

Posted on 2014-10-27
5
192 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
[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
  • Learn & ask questions
  • 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

729 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