如何从JavaScript中删除字符串中的文本? [英] How to remove text from a string in JavaScript?
本文介绍了如何从JavaScript中删除字符串中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,其中包含 data-123
作为其值。如何在 jQuery
或 Javascript
中进入并删除数据 - $ c离开
123
的字符串是$ c>?
I've got a string that has data-123
as its value. How in jQuery
or Javascript
would I go in and remove the data-
from the string while leaving the 123
?
推荐答案
否需要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:替换函数返回一个新字符串并保持原始字符串不变,因此使用函数返回在replace()调用之后的值。
PS: The replace function returns a new string and leaves the original string unchanged, so use the function return value after the replace() call.
这篇关于如何从JavaScript中删除字符串中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文