Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript regexp。替换除word以外的所有_Javascript_Html_Regex - Fatal编程技术网

javascript regexp。替换除word以外的所有

javascript regexp。替换除word以外的所有,javascript,html,regex,Javascript,Html,Regex,你好,我有一个元素类 <div class="help_1"></div> <div class="onemoreclass help_2 twoclass"></div> <div class="test help_3"></div> <div class="class1 help_4"></div> 如何使用javascript仅提取帮助匹配项?此解决方案需要jQuery库: var A =

你好,我有一个元素类

<div class="help_1"></div>
<div class="onemoreclass help_2 twoclass"></div>
<div class="test help_3"></div>
<div class="class1 help_4"></div>


如何使用javascript仅提取帮助匹配项?

此解决方案需要jQuery库:

var A = [];
$('div').each(function(){
  var B = $(this).attr('class').split(' ');
  for(var i=0;i<B.length;i++){
    var C = B[i];
    if( /^help_/.test(C) ){
      A.push(C);
    }
  }
});

console.log(A);
var A=[];
$('div')。每个(函数(){
var B=$(this.attr('class').split('');

for(var i=0;i用于针对属性类的直接javascript(假设您不使用jQuery,并且可以通过DOM定位有问题的元素)


$(t.currentTarget).attr('class').replace(/../g')是我的情况您想要提取字符串?或者您想要选择具有以该字符串开头的类的元素?您的答案需要解释这需要jQuery库。它离不需要jQuery库也只有两个标识符。这对于split()非常好但不是regexp?或者它不可能与regexp一起使用?@mindsupport:为什么你会与正则表达式结婚?为什么你认为你需要正则表达式来填补这个空白?Rustam正在使用正则表达式来搜索
帮助。\uuuu
,因此如果出于某些愚蠢的原因,你觉得必须使用正则表达式,那么你就可以得到保护。这比使用正则表达式更简单任何语言中只有一个regexp和.match.regexp都比简单的字符串运算符慢。不过,您还没有意识到这个解决方案的核心仍然是一个正则表达式(在
.test
中)。我不知道“使用javascript的动态内容”与此有什么关系。。?!
// Contains array of matches or null if none found
var matches= classAttr.match(/(help_\w)/g);