本文翻译自: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

如何从字符串中删除文本?相关推荐

  1. 从字符串中删除HTML标签

    是否有从Java字符串中删除HTML的好方法? 一个简单的正则表达式 replaceAll("\\<.*?>","") 可以使用,但& 不会 ...

  2. 如何使用JavaScript从字符串中删除空格?

    本文翻译自:How to remove spaces from a string using JavaScript? How to remove spaces in a string? 如何删除字符串 ...

  3. 在Java中从字符串中删除空格

    我有一个像这样的字符串: mysz = "name=john age=13 year=2001"; 我想删除字符串中的空格. 我试过trim()但这只删除了整个字符串前后的空格. ...

  4. 怎么批量删除html里的字段,如何从Excel中的字符串中删除所有HTML标签?

    如何从Excel中的字符串中删除所有HTML标签? 如果您的工作表包含用HTML标记括起来的文本字符串,现在,您想要从字符串中删除所有HTML标记,以使单元格清晰可见,如下面的屏幕截图所示. 本文,我 ...

  5. 使用CV2和Keras OCR从图像中删除文本

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 介绍 本文将讨论如何快速地从图像中删除文本,作为图像分类器的预处理 ...

  6. 从字符串中删除所有出现的char

    本文翻译自:Remove all occurrences of char from string I can use this: 我可以用这个: String str = "TextX Xt ...

  7. 如何从字符串中删除最后一个字符?

    我想从字符串中删除最后一个字符. 我尝试这样做: public String method(String str) {if (str.charAt(str.length()-1)=='x'){str ...

  8. 程序员面试题精选100题(36)-在字符串中删除特定的字符[算法]

    题目:输入两个字符串,从第一字符串中删除第二个字符串中所有的字符.例如,输入"They are students."和"aeiou",则删除之后的第一个字符串变 ...

  9. C语言字符串中删除重复的字符的算法(附完整源码)

    C语言字符串中删除重复的字符的算法 C语言字符串中删除重复的字符的算法完整源码(定义,实现,main函数测试) C语言字符串中删除重复的字符的算法完整源码(定义,实现,main函数测试) #inclu ...

最新文章

  1. python拟合曲线(小批量随机梯度下降)
  2. python手机版打了代码运行不了-三款可以在安卓手机上运行Python代码的软件
  3. UNITY2018 真机开启deepprofiling的操作
  4. 分布式mysql保持数据一致性_一种分布式跨数据库保持事务一致性的方法及系统与流程...
  5. 数据结构与算法之选择排序
  6. dptcpp 题目 2352: [信息学奥赛一本通-T1440]数的划分-dp
  7. unix/linux命令“ls -l”选项输出结果详解
  8. IntelliJ IDEA中文乱码解决办法
  9. YouTube上最火的OpenCV-Python入门视频教程
  10. 简单的TensorFlow分类教程
  11. 标注工具——VoTT的调试笔记
  12. 计算机械效率的公式四种,物理计算公式;
  13. 2010 年下半年系统分析师案例分析真题
  14. VSCode中Clangd无法找到stdio.h
  15. 如何在windows上 安装更新 显卡的驱动
  16. Win下的驱动备份与还原
  17. 计算机为啥启用不了网络发现,Win7“网络发现”功能启用不了的原因和解决方法...
  18. UE4编译错误:Unable to delete XXX
  19. openresty ngx_lua请求响应
  20. 【每天更新】2022年最新WordPress主题下载,外贸独立站商城/企业网站/个人博客模板 2022-5-18

热门文章

  1. 【Android】换肤技术讲解
  2. 大话android 进程通信之AIDL
  3. 如何开发一个属于自己的小程序
  4. OkHttp 源码解析---拦截器
  5. Ubuntu 修改截屏快捷键
  6. 调试技巧之 找准调试点
  7. 网易游戏2016实习生招聘笔试题目--推箱子
  8. PhoneGap揭开你的神秘面纱
  9. Android之View的绘制流程解析
  10. 魅族android p,2.Android P新特性实践-Slices