Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
split() with regExp \r
Old 06-13-2009, 04:35 AM split() with regExp \r
Skilled Talker

Posts: 52
Name: Bruce
Trades: 0
I'm wanting to split a string where there are carriage returns.

I'm using: var myStringArray = myString.split(/\r/)

Does anyone know why this isn't working?
SillyWilly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-13-2009, 10:14 AM Re: split() with regExp \r
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
try:
HTML Code:
<html>
<script type="text/javascript">
  myString='\nje suis\nvraiment tres\ncontent';
  var myStringArray=myString.split(String.fromCharCode(10));
  alert(myStringArray.length); //=> 4
</script>
</html>
10 being the ascii code for the carriage return.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-22-2009, 12:01 AM Re: split() with regExp \r
Skilled Talker

Posts: 52
Name: Bruce
Trades: 0
Thanks Tripy, but that's not working when used on a textnode with returns in it.
SillyWilly is offline
Reply With Quote
View Public Profile
 
Old 06-22-2009, 02:33 AM Re: split() with regExp \r
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Maybe it's related to your os....
try this:
Code:
<html>
<script type="text/javascript">
  myString='\nje suis\nvraiment tres\ncontent';
  var myStringArray=myString.split(String.fromCharCode(10));
  if(myStringArray.length==1){
    var myStringArray=myString.split(String.fromCharCode(13));
  }
  alert(myStringArray.length); //=> 4
</script>
</html>
the ascii code 10 is "carriage return", the ascii code 13 is "line feed".
Windows use cr+lr (\r\n) as end of line.
Linux use only cr (\r), mac use only lf (\n).

So, maybe your problem could be related to the os the text was stored from.
I'm not sure, but it's worth a try, I think.

And maybe if you could post a bit of code, I could help you more.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to split() with regExp \r
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17283 seconds with 12 queries