简体   繁体   English

如何使用JavaScript替换字符串中的最后一个匹配字符

[英]How to replace last matched character in string using javascript

i want to replace last input character from keyboard to '' 我想将键盘上的最后一个输入字符替换为''

My String Input are 我的字符串输入是

sample string 样品串

"<p><strong>abscd sample text</strong></p>"

"<p>abscd sample text!</p>"

My last character is dynamic that can be any thing between a to z, A to Z, 0 to 9, any special characters([~ / < > & ( . ] ). So i need to replace just that character 我的最后一个字符是动态的,可以是介于a到z,A到Z,0到9之间的任何字符,也可以是任何特殊字符([〜/ <>&(。])。所以我只需要替换该字符

for example in Sample 1 i need to replace "t" and in sample 2 in need to replace "!" 例如,在示例1中,我需要替换“ t”,而在示例2中,我需要替换“!”

I tried below code. 我尝试下面的代码。 but it id not worked for me 但它对我不起作用

 var replace = '/'+somechar+'$/';

Any way to do it? 有办法吗?

Step one 第一步

to replace the a character in a string, use replace() function of javaScript. 要替换字符串中的字符,请使用javaScript的replace()函数。 Here is the MDN specification: 是MDN规范:

Returns a new string with some or all matches of a pattern replaced by a replacement. 返回一个新字符串,其中部分或全部匹配的模式被替换。 The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. 模式可以是字符串或RegExp,而替换项可以是字符串或每个匹配项要调用的函数。

Step two 第二步

you need to location the character to be replaced through regular expression. 您需要定位要通过正则表达式替换的字符。 You want to replace the last character of a string and this could be expressed as /(.+)(.)$/. 您要替换字符串的最后一个字符 ,并且可以将其表示为/(.+)(.)$/。 . stands for any character, + means more than one character. 代表任何字符, +表示多个字符。 Here (.+) matches all the character before the last one. 此处(.+)匹配最后一个字符之前的所有字符。 (.) matches the last character. (.)匹配最后一个字符。

What you want to replace is the one inside the second brackets. 您要替换的是第二个括号内的那个。 Thus you use the same string matched in the first bracket with $1 and replace whatever after it. 因此,您可以使用第一个括号中与$1匹配的相同字符串,并替换其后的任何内容。

Here is the code to realize your intention: 这是实现您的意图的代码:

text = 'abscd sample text';
text.replace(/(.+)(.)$/, '$1!');

Do you really need to use regular expressions? 您真的需要使用正则表达式吗? How about str = str.slice(0, -1); 那么str = str.slice(0, -1); ? This will remove the last character. 这将删除最后一个字符。

If you need to replace a specific character, do it like this: 如果需要替换特定字符,请按照以下步骤操作:

var replace = new RegExp(somechar + '$');
str = str.replace(replace, '');

You cannot use slashes in a string to construct a RegEx. 您不能在字符串中使用斜杠来构造RegEx。 This is different from PHP, for example. 例如,这与PHP不同。

I dont really understand which character you want to replace to what, but i think, you should use replace() function in JS: http://w3schools.com/jsref/jsref_replace.asp 我不太了解要替换为哪个字符,但是我认为您应该在JS中使用replace()函数: http : //w3schools.com/jsref/jsref_replace.asp

string.replace(regexp/substr,newstring)

This means all keyboard character: 这意味着所有键盘字符:

[\t\n ./<>?;:"'`!@#$%^&*()[]{}_+=-|\\] 

And this way you can replace all keyboard character before < mark to "" 这样,您可以将<标记之前的所有键盘字符替换为""

 string.replace("[a-zA-Z0-9\t\n ./<>?;:"'`!@#$%^&*()[]{}_+=-|\\]<","<")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM