split not working if pass split character as variable

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
ethar1Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
leakim971Connect With a Mentor PluritechnicianCommented:
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
 
ethar1Author Commented:
could you please explain "in your example \n is \\n and NOT the new line character"

thanks,
0
 
leakim971PluritechnicianCommented:
\ 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
ethar1Author Commented:
thanks
0
 
leakim971PluritechnicianCommented:
'welcome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.