Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

split not working if pass split character as variable

Posted on 2013-12-28
6
Medium Priority
?
212 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 83

Accepted Solution

by:
leakim971 earned 2000 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 83

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ethar1
ID: 39745203
thanks
0
 
LVL 83

Expert Comment

by:leakim971
ID: 39745324
'welcome
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

877 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