Remove all the characters after $ sign using Javascript
I want to remove all the characters that appear after "$" sign in my string using javascript.
Is there any function in javascript which can help me achieve this. I am quite开发者_如何学Python new to client side scripting.
Thanks.
How about this
astr.split("$")[0];
NB This will get you all of the characters up to the $
. If you want that character too you will have to append it to this result.
You can try this regex, it will replace the first occurance of $
and everything after it with a $
.
str.replace(/\$.*/, '$');
Input: I have $100
Output: I have $
there are a few different ways
var myStr = "asdasrasdasd$hdghdfgsdfgf";
myStr = myStr.split("$")[0];
Or
var myStr = "asdasrasdasd$hdghdfgsdfgf";
myStr = myStr.substring(0, myStr.indexOf("$") - 1);
You need to get the substring and pass the index of the $
as it's second parameter.
var newString = oldString.substring(0, oldString.indexOf("$", 0))
Use the subtring and indexOf methods like so:
var someString = "12345$67890";
alert(someString.substring(0, someString.indexOf('$')));
jsFiddle example
Use .split()
to break it up at the dollar signs and then grab the first chunk:
var oldstring = "my epic string $ more stuff";
var split = oldstring.split("$");
var newstring = split[0] + "$";
alert(newstring); //outputs "my epic string $"
Regular expressions are very helpful:
/([^$]*\$?)/.exec("aa$bc")[1] === "aa$"
精彩评论