如何从字符串中删除文本?

本文翻译自:How to remove text from a string?

I've got a data-123 string. 我有一个data-123字符串。

How can I remove data- from the string while leaving the 123 ? 我如何在离开123从字符串中删除data-


#1楼

参考:https://stackoom.com/question/hdEh/如何从字符串中删除文本


#2楼

No jQuery needed. 无需jQuery。

 var ret = "data-123".replace('data-',''); console.log(ret); //prints: 123 

Docs. 文件。


For all occurrences to be discarded use: 对于所有要丢弃的事件,请使用:

var ret = "data-123".replace(/data-/g,'');

PS: The replace function returns a new string and leaves the original string unchanged, so use the function return value after the replace() call. PS:replace函数返回一个新字符串,并使原始字符串保持不变,因此请在replace()调用之后使用该函数的返回值。


#3楼

Plain old JavaScript will suffice - jQuery is not necessary for such a simple task: 普通的旧JavaScript就足够了-jQuery对于这样的简单任务不是必需的:

var myString = "data-123";
var myNewString = myString.replace("data-", "");

See: .replace() docs on MDN for additional information and usage. 有关其他信息和用法, 请参阅: 有关MDN的.replace()文档


#4楼

This doesn't have anything to do with jQuery. 这与jQuery没有任何关系。 You can use the JavaScript replace function for this: 您可以为此使用JavaScript replace功能:

var str = "data-123";
str = str.replace("data-", "");

You can also pass a regex to this function. 您也可以将正则表达式传递给此函数。 In the following example, it would replace everything except numerics: 在以下示例中,它将替换数字以外的所有内容:

str = str.replace(/[^0-9\.]+/g, "");

#5楼

I was used to the C# (Sharp) String.Remove method. 我习惯了C#(Sharp)String.Remove方法。 In Javascript, there is no remove function for string, but there is substr function. 在Javascript中,没有用于字符串的remove函数,但是有substr函数。 You can use the substr function once or twice to remove characters from string. 您可以使用一次或两次substr函数从字符串中删除字符。 You can make the following function to remove characters at start index to the end of string, just like the c# method first overload String.Remove(int startIndex): 您可以执行以下函数来删除字符串的开始索引处到字符串末尾的字符,就像c#方法的第一个重载String.Remove(int startIndex)一样:

function Remove(str, startIndex) {
    return str.substr(0, startIndex);
}

and/or you also can make the following function to remove characters at start index and count, just like the c# method second overload String.Remove(int startIndex, int count): 和/或您还可以使以下函数删除起始索引和计数处的字符,就像c#方法第二次重载String.Remove(int startIndex,int count)一样:

function Remove(str, startIndex, count) {
    return str.substr(0, startIndex) + str.substr(startIndex + count);
}

and then you can use these two functions or one of them for your needs! 然后您可以使用这两个功能或其中之一来满足您的需要!

Example: 例:

alert(Remove("data-123", 0, 5));

Output: 123 输出:123


#6楼

You can use "data-123".replace('data-',''); 您可以使用"data-123".replace('data-',''); , as mentioned, but as replace() only replaces the FIRST instance of the matching text, if your string was something like "data-123data-" then ,如前所述,但是replace()仅替换匹配文本的FIRST实例,如果您的字符串类似于"data-123data-"

"data-123data-".replace('data-','');

will only replace the first matching text. 只会替换第一个匹配的文本。 And your output will be "123data-" 您的输出将为"123data-"

DEMO 演示

So if you want all matches of text to be replaced in string you have to use a regular expression with the g flag like that: 因此,如果您希望将所有文本匹配替换为字符串,则必须使用带有g标志的正则表达式,如下所示:

"data-123data-".replace(/data-/g,'');

And your output will be "123" 您的输出将为"123"

DEMO2 演示2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值