coffeescript-cookbook / coffeescript-cookbook.github.io

CoffeeScript Recipes, Examples and Tutorials
https://coffeescript-cookbook.github.io
Other
562 stars 178 forks source link

A simpler, more elegant solution than using regexes #138

Closed Data-Meister closed 8 years ago

Data-Meister commented 8 years ago

Added benefit of not worrying about regex escaping.

Data-Meister commented 8 years ago

P.S. I've got some similar code for doing a substring replace

newString = originalStr.split( subStr ).join( replacementStr )

I could PR this in the same place. Or would it go better in another chapter?

lolmaus commented 8 years ago

And this is also faster: http://jsperf.com/occurrences-of-substring

But your commit is wrong.

message.split " a " .length - 1

would compile to

message.split(" a ".length - 1);

while we want

message.split(" a ").length - 1;

Thus, you want either of these:

message.split(" a ").length - 1

message
  .split " a "
  .length - 1
Data-Meister commented 8 years ago

Cheers! I'll redo the PR with that corrected.

Shall I include the string replace code?

lolmaus commented 8 years ago

Shall I include the string replace code?

Wouldn't hurt! But check if there's a better section to place it into.

sukima commented 8 years ago

Why did this close? A pull request can be appended and modified by rebaseing and force pushing to the original branch.

This is a down side to using GitHub web interface over the Git command line tool.

Data-Meister commented 8 years ago

Oh right, thanks for the tip sukima. I closed it with the intention of reverting then re committing. Will do this tonight hopefully.