Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

split method in string

 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a simple string and want to split the string based on a regex. I am using split method to split the string
For Example:


Here the array size is zero.

If i replace with str="fname,lname"; and use str.split(",");

It works fine. Is there anything wrong with the period.

Thanks,
Srikanth.
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works fine. Is there anything wrong with the period.


A period has special meaning in a regex. It means to match anything. So, if you want to match an actual period, you will need to escape it... use "\\." instead.

Henry
 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Huh! it worked.
Thanks Henry.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:


A period has special meaning in a regex. It means to match anything...


Except new line characters (by default).
 
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!