Javascript 如何从字符串中删除文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10398931/
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
How to remove text from a string?
提问by Michael Grigsby
I've got a data-123
string.
我有一个data-123
字符串。
How can I remove data-
from the string while leaving the 123
?
data-
离开时如何从字符串中删除123
?
回答by Mathletics
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()调用后的函数返回值。
回答by James Johnson
This doesn't have anything to do with jQuery. You can use the JavaScript replace
function for this:
这与 jQuery 没有任何关系。您可以replace
为此使用 JavaScript函数:
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, "");
回答by laaposto
You can use "data-123".replace('data-','');
, as mentioned, but as replace()
only replaces the FIRSTinstance of the matching text, if your string was something like "data-123data-"
then
您可以使用"data-123".replace('data-','');
,如提及,但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"
回答by m.r shojaei
you can use slice() it returens charcters between start to end (included end point)
您可以使用 slice() 它在开始到结束之间返回字符(包括终点)
string.slice(start , end);
here is some exmp to show how it works:
这里有一些例子来展示它是如何工作的:
var mystr = ("data-123").slice(5); // jast define start point so output is "123"
var mystr = ("data-123").slice(5,7); // define start and end so output is "12"
var mystr=(",246").slice(1); // returens "246"
回答by James Hill
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 MDNfor additional information and usage.
有关其他信息和用法,请参阅:.replace()
MDN 上的文档。
回答by Ranjit Kanojia
Ex:-
前任:-
var value="Data-123";
var removeData=value.replace("Data-","");
alert(removeData);
Hopefully this will work for you.
希望这对你有用。
回答by Ranjit Kanojia
I was used to the C# (Sharp) String.Remove method. In Javascript, there is no remove function for string, but there is substr function. You can use the substr function once or twice to remove characters from string. 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# (Sharp) String.Remove 方法。在 Javascript 中,没有删除字符串的函数,但是有 substr 函数。您可以使用 substr 函数一次或两次从字符串中删除字符。您可以使用以下函数将起始索引处的字符删除到字符串末尾,就像 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
回答by Ranjit Kanojia
This little function I made has always worked for me :)
我做的这个小功能一直对我有用:)
String.prototype.deleteWord = function (searchTerm) {
var str = this;
var n = str.search(searchTerm);
while (str.search(searchTerm) > -1) {
n = str.search(searchTerm);
str = str.substring(0, n) + str.substring(n + searchTerm.length, str.length);
}
return str;
}
// Use it like this:
var string = "text is the the cool!!";
string.deleteWord('the'); // Returns text is cool!!
I know it is not the best, but It has always worked for me :)
我知道它不是最好的,但它一直对我有用:)
回答by ARC
str.split('Yes').join('No');
This will replace all the occurrences of that specific string from original string.
这将从原始字符串中替换该特定字符串的所有出现。
回答by Brett DeWoody
Using match()
and Number()
to return a number
variable:
使用match()
和Number()
返回一个number
变量:
Number(("data-123").match(/\d+$/));
// strNum = 123
Here's what the statement above does...working middle-out:
这是上面的语句所做的......工作中间:
str.match(/\d+$/)
- returns an array containing matches to any length of numbers at the end ofstr
. In this case it returns an array containing a single string item['123']
.Number()
- converts it to a number type. Because the array returned from.match()
contains a single elementNumber()
will return the number.
str.match(/\d+$/)
- 返回一个包含匹配任何长度数字的数组str
。在这种情况下,它返回一个包含单个字符串 item 的数组['123']
。Number()
- 将其转换为数字类型。因为从.match()
包含单个元素Number()
返回的数组将返回数字。