I wonder how can i remove all string part after "#".
For example if i have example.com/hello#item1 how can i remove all characteres after # and # character too?
-
you got 3 completely different ways to do it... take your pickBilly Moon– Billy Moon2011-08-09 18:31:12 +00:00Commented Aug 9, 2011 at 18:31
-
...and none of them involve jQuery, of course.Frédéric Hamidi– Frédéric Hamidi2011-08-09 18:32:19 +00:00Commented Aug 9, 2011 at 18:32
-
Good to know. Just involve JavaScript?krzyhub– krzyhub2011-08-09 18:34:27 +00:00Commented Aug 9, 2011 at 18:34
-
@Cris, indeed. You don't need jQuery to manipulate strings unless you want to trim them, and even that can be done with a simple regex too.Frédéric Hamidi– Frédéric Hamidi2011-08-09 18:37:19 +00:00Commented Aug 9, 2011 at 18:37
-
Regex are the same in most of languages?krzyhub– krzyhub2011-08-09 18:42:26 +00:00Commented Aug 9, 2011 at 18:42
|
Show 1 more comment
6 Answers
var string = "example.com/hello#item1".split('#')[0];
simple as that
2 Comments
JochenJung
Note, that string will be undefined, when # is not found. You might think, that it should return the whole string in that case instead.
Senad Meškin
it will always return whole string.
You can just use split.
var oldString ='somestring#removethis';
var newString = oldString.split('#', 1)[0];
alert(newString);
Comments
Try this:
var str = "example.com/hello#item1";
var newStr = str.substring(0, (str.length - str.indexOf("#")));
1 Comment
Billy Moon
is this the opposite of code golf? what would that be called..? code flog?