简体   繁体   English

如何在javascript中删除字符串末尾的某个字符

[英]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.

相关问题 如何使用javascript或jquery隐藏某个字符后的字符串结尾 - How to hide the end of a string after a certain character, with javascript or jquery 如何从Javascript中的字符串中删除以某个字符开头的单词 - How to remove a word that starts with a certain character from a string in Javascript 在动态字符串中,如何在特定字符处开始子字符串并在特定字符处结束子字符串? - In a dynamic string, how to start a substring at a certain character and end it at certain character? 如何判断一个字符串是否包含JavaScript中的某个字符? - How to tell if a string contains a certain character in JavaScript? 使用JavaScript删除字符串中某些字符之后的所有内容 - Remove Everything After Certain Character Within String Using JavaScript 如何从 JavaScript 中的某些子字符串(以列表形式给出)以外的字符串中删除每个字符? - How to remove every character from a string except certain substrings (given as a list) in JavaScript? 删除以某些字符开头的字符串 - Remove string starting with certain character 如何从javascript中的字符串末尾删除9个字符 - How to remove 9 characters from the end of the string in javascript Javascript:如何从字符串末尾删除标点符号 - Javascript: how to remove punctuation from the end of a string 如何删除`//<![CDATA[` and end `//]]> ` 使用字符串中的 javascript? - How to remove `//<![CDATA[` and end `//]]>` with javascript from string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM