博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何从字符串中删除文本?
阅读量:2289 次
发布时间:2019-05-09

本文共 3068 字,大约阅读时间需要 10 分钟。

本文翻译自:

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

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


#1楼

参考:


#2楼

No jQuery needed. 无需jQuery。

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


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: for additional information and usage. 有关其他信息和用法, 请参阅:


#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-"

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"

转载地址:http://wpcnb.baihongyu.com/

你可能感兴趣的文章
从Java码农到年薪八十万的架构师
查看>>
python psutil结合钉钉报警
查看>>
一键升级python
查看>>
python 99乘法表
查看>>
一个可以拿来直接用的资产管理项目
查看>>
Centos 7 firewalld 基本操作
查看>>
passwd:只能指定一个用户的名称。
查看>>
nginx日志切割和日志清理
查看>>
RPC与分布式服务框架Dubbo
查看>>
为什么需要文件服务器?
查看>>
Redis怎么实现主从同步的
查看>>
Spring整理
查看>>
Spring Mvc整理
查看>>
Dubbo整理
查看>>
Redis整理
查看>>
JVM内存模型和类加载机制
查看>>
JDK1.0到12各版本新特性
查看>>
JAVA的一次编译,到处运行。可以在所有的平台上运行?
查看>>
HttpSessionListener监听器
查看>>
JSP
查看>>