如何使用数组删除字符串的一部分 [英] How to remove a part of the string using arrays

查看:109
本文介绍了如何使用数组删除字符串的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要在字符串上找到int, float, char, bool, main, void main, int main, cout, cin""的话,我想将其从技术上删除(如技术上的替换).

I want to remove(technically replacing) the words int, float, char, bool, main, void main, int main, cout, cin to "" (like removing it) if it is found on the string.

所以,如果我有ff:

str = "void main(){  couts<<"wrong"; cout<<"right";  }"

替换后,应该是:

str = "(){  ();  couts<<"wrong"; <<"right"; }"

int,float,char,bool,main ...等单词存储在数组中

The words int, float, char, bool, main... etc are stored inside an array

kWord[0] = /int/
kWord[1] = /float/
kWord[2] = /char/
kWord[3] = /bool/
kWord[4] = /main/
kWord[5] = /void\s+main/
kWord[6] = /int\s+main/
kWord[7] = /cout/
kWord[8] = /cin/

这与我以前的问题有关

This is related to my previous question How to remove a part of the string the fastest way but this time the words are in Array

推荐答案

您可以执行与

You can do the same as in the answer to your other question, but building a regex object using kWord.join('|').

kWord[0] = 'int';
kWord[1] = 'float';
kWord[2] = 'char';
kWord[3] = 'bool';
kWord[4] = 'main';
kWord[5] = 'void\\s+main';
kWord[6] = 'int\\s+main';
kWord[7] = 'cout';
kWord[8] = 'cin';

var r = '\\b(' + kWord.join('|') + ')\\b';
var myRegex = new RegExp(r, 'g');
str = str.replace(myRegex, "");

这篇关于如何使用数组删除字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆