javascript 从字符串中删除一个字母(:)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7921204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:43:35  来源:igfitidea点击:

Remove a letter(:) from a string

javascriptjquery

提问by Sandy

I have strings like Name:, Call:, Phone:....and so on in my table. I am learning jQuery and was able to access the text. My tutorial has used trim()to remove any whitespaces. But I want o remove ":" from the end of each string (and yes, it always lies in the end after calling trim()method). So how to achieve it.

我的表中有名称:,呼叫:,电话:...等字符串。我正在学习 jQuery 并且能够访问文本。我的教程用于trim()删除任何空格。但我想从每个字符串的末尾删除“:”(是的,它总是在调用trim()方法后的末尾)。那么如何实现呢。

Its my code:

它的我的代码:

<script type="text/javascript">
    $(function ()
    {
        $(':input[type=text], textarea').each
    (
    function ()
    {
        var newText = 'Please enter your ' +
    $(this).parent().prev().text().toLowerCase().trim();
        $(this).attr('value', newText);
    }).one('focus', function ()
    {
        this.value = '', this.className = ''
    }).addClass('Watermark').css('width', '300px');
    });
</script>

trim(":") did not help...

修剪(“:”)没有帮助......

回答by Blender

You can replace all :characters:

您可以替换所有:字符:

var str = '::a:sd:';
str = str.replace(/:/g,''); // str = 'asd';

Or use a handy rtrim()function:

或者使用一个方便的rtrim()功能:

String.prototype.rtrim = function(character) {
    var re = new RegExp(character + '*$', 'g');
    return this.replace(re, '');
};

var str = '::a:sd:';
str = str.rtrim(':'); // str = '::a:sd';

回答by weir

In this case just use the plain old JavaScript replaceor substrmethods.

在这种情况下,只需使用普通的旧 JavaScriptreplacesubstr方法。

You can also use a regular expression that looks for colon as the last character (the character preceding the regexp end-of-string anchor"$").

您还可以使用正则表达式来查找冒号作为最后一个字符(正则表达式字符串结尾锚点“$”之前的字符)。

"hi:".replace(/:$/, "")

hi

你好

"hi".replace(/:$/, "")

hi

你好

"h:i".replace(/:$/, "")

h:i

你好

This is a simplified, inline version of the rtrimfunction in Blender's answer.

这是rtrimBlender 答案中函数的简化内联版本。

EDIT: Here is a test fiddle for Blender's corrected rtrimfunction. Note that his RegExp will delete multiple occurrences of the specified character if the string ends with multiple instances of it consecutively (example bolded below).

编辑:这是 Blender 修正rtrim功能的测试小提琴。请注意,如果字符串以连续的多个实例结尾,则他的 RegExp 将删除指定字符的多次出现(下面以粗体显示的示例)。

http://jsfiddle.net/fGrPb/5/

http://jsfiddle.net/fGrPb/5/

input = '::a:sd:' output = '::a:sd'; input = 'hi:' output = 'hi'; input = 'hi:::' output = 'hi'; input = 'hi' output = 'hi'; input = 'h:i' output = 'h:i'

输入 = '::a:sd:' 输出 = '::a:sd'; 输入 = '你好:' 输出 = '你好'; 输入 = '嗨:::' 输出 = '嗨'; 输入 = '嗨' 输出 = '嗨'; 输入 = 'h:i' 输出 = 'h:i'

回答by Nick Long

To chop the last character of a string use string.slice(0,-1)

要截断字符串的最后一个字符,请使用 string.slice(0,-1)

回答by FreeCandies

var myStr = "something:";
myStr = myStr.slice(0, -1);

回答by Peter Olson

This checks if the last character is a colon. If it is, the last character is removed.

这将检查最后一个字符是否为冒号。如果是,则删除最后一个字符。

if (str[str.length - 1] === ":") {
  str = str.slice(0, -1);
}

If there can be multiple trailing colons, you can replace ifwith while, like this:

如果可以有多个尾随冒号,则可以替换ifwhile,如下所示:

while (str[str.length - 1] === ":") {
  str = str.slice(0, -1);
}

You could even make a generic trimfunction that accepts a string and a character and trims trailing instances of that character:

您甚至可以创建一个通用trim函数,它接受一个字符串和一个字符并修剪该字符的尾随实例:

var trim = function(str, chr) {
  while (str[str.length - 1] === ":") {
    str = str.slice(0, -1);
  }
  return str;
}

回答by Ergec

var a="name:";
var b=a.split(":");
alert(b[0]);

回答by Rafay

one way is to use lastIndexOf

一种方法是使用 lastIndexOf

var str='Name:, Call:, Phone:';
var index=str.lastIndexOf(":");
alert(index);
var s=str.substring(0,index);
alert(s);

DEMO

演示

回答by Matt K

You can use a regular expression to remove the colon (:).


Replace one instance:

您可以使用正则表达式删除冒号 (:)。


替换一个实例:

var with_colon = 'Stuff:';
var regex = /([^:]*):/; 
var without_colon = regex.exec(with_colon)[1];
alert(without_colon);

Result: Stuff


Replace all instances:

结果:Stuff


替换所有实例:

var with_colon = 'Stuff: Things:'; 
var without_colon = with_colon.replace(/([^:]*):/g,''); 
alert(without_colon);

Result: Stuff Things

结果: Stuff Things

回答by Scott Hunter

str = str.substring(0,str.lastIndexOf(":"));

str = str.substring(0,str.lastIndexOf(":"));

Note that this removes everything from the last :to the end of the string (for example, any whitespace after the :).

请注意,这将删除从:字符串末尾到末尾的所有内容(例如, 之后的任何空格:)。

回答by r15habh

 function trim(str) {
       str = str.replace(/^:*/,"");
       return str.replace(/:*$/,"");
 }