本指南演示了如何使用jQuery从字符串中删除子字符串。 这些示例利用了jQuery的grep()
函数,提供了一种类似于PHP的子字符串操作功能的灵活方法。 使用Firebug或您的浏览器的开发人员工具对代码进行测试和实验。
(function($) { var myFruits = "Apples, Bananas, Mangos, Blackberries, Oranges"; myFruits = myFruits.replace(/bMangos(, |$)/gi, ""); myFruits = "Apples, Bananas, Mangos, Blackberries, Oranges"; var result = $.grep(myFruits.split(', '), function(v) { return v != "Mangos"; }).join(', '); console.log(result); function filterOut(my_str, t) { //string, term return $.grep(my_str.split(', '), function(v) { return v != t; }).join(', '); } })(jQuery); //output: Apples, Bananas, Blackberries, Oranges
> jQuery字符串操纵上的常见问题(常见问题解答) 本节解决了有关操纵jQuery中的字符串的常见问题。
>>
问:如何使用jQuery从字符串中删除特定字符?a:>使用
>方法。 例如:
replace()
问:如何删除字符的多个实例?
var str = "Hello, World!"; str = str.replace(",", ""); // Removes the comma
>在>方法的正则表达式中使用全局标志():
Q:如何删除子字符串?
g
>replace()
a:
var str = "Hello, World, Hello!"; str = str.replace(/,/g, ""); // Removes all commas
方法也适用于子字符串:>
问:如何删除case-insensitionally?
>a:replace()
>使用案例不敏感的正则表达式(
var str = "Hello, World!"; str = str.replace("World", ""); // Removes "World"
问:如何删除第一个字符? >a:
>使用>方法:>
i
>
var str = "Hello, World!"; str = str.replace(/world/i, ""); // Removes "World" regardless of case
a:>使用>方法:
>
问:如何在特定位置上删除字符?
substring()
>
var str = "Hello, World!"; str = str.substring(1); // Removes the "H"
两次:>
问:如何删除所有空间?
>a:slice()
使用与所有空格字符匹配的正则表达式:
var str = "Hello, World!"; str = str.slice(0, -1); // Removes the "!"
问:如何删除所有非alphanumeric字符? >
a:使用匹配非alphanumeric字符的正则表达式:
slice()
var str = "Hello, World!"; str = str.slice(0, 5) + str.slice(6); // Removes the character at position 6 (index 5)
a:>使用将匹配固定到字符串末端()末端的正则表达式:
以上是jQuery从字符串中删除字符串的详细内容。更多信息请关注PHP中文网其他相关文章!