• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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
0
ethar1
Asked:
ethar1
  • 3
  • 3
1 Solution
 
leakim971PluritechnicianCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ethar1Author Commented:
thanks
0
 
leakim971PluritechnicianCommented:
'welcome
0

Featured Post

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.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now