简体   繁体   English

正则表达式替换器不起作用

[英]Regular Expression Replacer Not Working

My problem is that I cannot get the code to remove mark tags around a word and keep the word there with the same capitalization. 我的问题是我无法获取代码来删除单词周围的标记标签,并以相同的大小写形式保留单词。 Here is my code: 这是我的代码:

function removeMark(tagName, Color) {
  var tagNameC = tagName.toUpperCase();
  var IndAry = tags.indexOf(tagNameC);
  tags.splice(IndAry, 1);
  var Notes = document.getElementById("NoteHolder").innerHTML;
  var MarkBack = new RegExp("/b" + tagName + "/b", "gim")
  var MarkBackTo = "<mark class=" + Color + ">" + MarkBack + "</mark>"
  var AfterRep = Notes.replace(MarkBackTo, "$1");
  document.getElementById("NoteHolder").innerHTML = AfterRep;
  var list = document.getElementById("myList");
  var idmarker = "MarkID" + tagName;
  var ElementID = document.getElementById(idmarker);
  MarkList.removeChild(ElementID);
}

tagName is the word inside the mark tags. tagName是标记标签中的单词。 The mark tags have a class that is a constant but the word inside may not always be due to capitalization. 标记标签具有恒定的类,但是里面的单词可能并不总是由于大写而引起的。 I am inputting tagName and Color through the input. 我正在通过输入输入tagNameColor I am guessing it has to do with the regex since I do not know much about it and I cannot find anything wrong with my javascript. 我想这与正则表达式有关,因为我对此不太了解,而且我的JavaScript找不到任何错误。 If anyone can see what is going wrong it would be extremely helpful! 如果有人看到问题所在,那将非常有帮助!

var input = '<mark class="color">coTNeNt</mark>'
var reg = /<([\w]+)[ \w=\"]+>([\w]+)<\/\1>/g
input.replace(reg, '$2')

Output: "coTNeNt" 输出: "coTNeNt"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM