Solved

split not working if pass split character as variable

Posted on 2013-12-28
6
172 Views
Last Modified: 2013-12-30
Dear all,
Please take a look at this code:
http://jsfiddle.net/ethar1/8FBA9/


split function not working as expected  if passed as parameter

thanks
0
Comment
Question by:ethar1
  • 3
  • 3
6 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39744164
in your example \n is \\n and NOT the new line character.
You may...should use a regular expression to achieve your goal : http://jsfiddle.net/aA45X/

function RunSplit(sep, data) {
    sep = new RegExp(sep);
    var items = data.replace("(?m)^[ \t]*\r?\n", "");
    var lines = items.replace(/,$/, "").split(sep);
    $('#result').text( lines.length );
}

Open in new window

0
 

Author Comment

by:ethar1
ID: 39744179
could you please explain "in your example \n is \\n and NOT the new line character"

thanks,
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39744193
\ is the escape character when you're using it in a string in your code :
var str = "_______\n_______"; // here \n is a single character

but in textbox, you type two characters : \ followed by n
the \ is not the escape character
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:ethar1
ID: 39745203
thanks
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39745324
'welcome
0
 

Author Comment

by:ethar1
ID: 39747132
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

706 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