[英]Remove all the characters after $ sign using Javascript
I want to remove all the characters that appear after "$" sign in my string using javascript. 我想删除使用javascript在我的字符串中“$”签名后出现的所有字符。
Is there any function in javascript which can help me achieve this. 在javascript中是否有任何功能可以帮助我实现这一目标。 I am quite new to client side scripting.
我是客户端脚本的新手。
Thanks. 谢谢。
How about this 这个怎么样
astr.split("$")[0];
NB This will get you all of the characters up to the $
. NB这将为您提供最多
$
所有角色。 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
输入:
I have $100
Output: I have $
输出:
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);
您需要获取子字符串并传递$
的索引作为它的第二个参数。
var newString = oldString.substring(0, oldString.indexOf("$", 0))
Use the subtring and indexOf methods like so: 使用子字符串和indexOf方法,如下所示:
var someString = "12345$67890";
alert(someString.substring(0, someString.indexOf('$')));
Use .split()
to break it up at the dollar signs and then grab the first chunk: 使用
.split()
将其分解为美元符号,然后获取第一个块:
var oldstring = "my epic string $ more stuff";
var split = oldstring.split("$");
var newstring = split[0] + "$";
alert(newstring); //outputs "my epic string $"
正则表达式非常有用:
/([^$]*\$?)/.exec("aa$bc")[1] === "aa$"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.