[英]how to remove a certain character at the end of the string in javascript
http://dren.ch/js_blowfish/ http://dren.ch/js_blowfish/
I'm experimenting with blowfishJS, but it has a bug that makes it unusable 我正在尝试blowfishJS,但是它有一个错误使其无法使用
//lets say I want to encrypt the string "house" //让我说我想加密字符串“ house”
var bf = new Blowfish('some key');
var house = "house";
var ciphertext = bf.encrypt(house);
var plaintext = bf.decrypt(ciphertext);
console.log(plaintext); //outputs house00 instead of house
sometimes its more than 2 zeros. 有时会超过2个零。
This might be helpful: 这可能会有所帮助:
All the strings im trying to encrypt end with "=" anyway. 无论如何,im试图加密的所有字符串都以“ =”结尾。 so it would be ok to remove all the zeros at the end of the "plaintext" string until the character "=" shows up. 因此可以删除“明文”字符串末尾的所有零,直到出现字符“ =”。
How do I achieve this? 我该如何实现? lets say I have the string "mystring==000" and I need to remove all the zeros at the end. 可以说我有字符串“ mystring == 000”,并且需要在最后删除所有零。 I have done my research on the "slice" function, the problem is that there is no certain "position" at the end since I can't know if 2,3 or x zeros will appear 我已经对“切片”函数进行了研究,问题是最后没有确定的“位置”,因为我不知道是否会出现2,3或x零
You could use lastIndexOf()
and substr
from that index. 您可以使用lastIndexOf()
和该索引的substr
。 for example; 例如;
var house = 'house=04543976439859df45345ffd43';
house.substr( 0, house.lastIndexOf('=') + 1 ); // => "house="
lastIndexOf
will return the index of the last occurrence of the specified value (I do + 1
in this example to include the =
character). lastIndexOf
将返回指定值最后一次出现的索引(在此示例中,我做+ 1
以包含=
字符)。 Which we can then use with substr
to extract all characters after that index. 然后我们可以将它与substr
一起使用,以提取该索引之后的所有字符。
如果您确定字符串以零结尾并且可以修剪直到=使用,则可以使用正则表达式来执行此操作
plaintext.replace(/=(0)+$/g,"=");
With a regex, you can remove anything after the last = regardless of being a zero. 使用正则表达式,您可以删除last =之后的所有内容,而不管其为零。
plaintext.replace(/[^\\=]+$/, ""); plaintext.replace(/ [^ \\ =] + $ /,“”);
In plain english that is, find a maximal substring that doesn't include an '=' and it's at the end of the string and replace it for an empty string. 用简单的英语来说,就是找到一个不包含“ =”且位于字符串末尾的最大子字符串,并将其替换为空字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.