This question already has an answer here: RegEx match open tags except XHTML self-contained tags 35 answers The test method returns true for match2 because it has found a match . In order to fix it, change your regex this way: ^(?:<(w+)(?:(?:s+w+(?:s*=s*(?:".*?"|'.*?'|[^'">s]+))?)+s*|s*)>[^<>]*</1+s*>|<w+(?:(?:s+w+(?:s*=s*(?:".*?"|'.*?'|[^'">s]+))?)+s*|s*)/>|&
这个问题在这里已经有了答案: RegEx匹配除XHTML自包含标签之外的开放标签35个答案 test方法为match2返回true,因为它找到了匹配项 。 为了解决它,以这种方式改变你的正则表达式: ^(?:<(w+)(?:(?:s+w+(?:s*=s*(?:".*?"|'.*?'|[^'">s]+))?)+s*|s*)>[^<>]*</1+s*>|<w+(?:(?:s+w+(?:s*=s*(?:".*?"|'.*?'|[^'">s]+))?)+s*|s*)/>|<!--.*?-->|[^<>]+)*$ 描述(点击放大) 演示
This question already has an answer here: RegEx match open tags except XHTML self-contained tags 35 answers You want to remove a <div> element from your document? First things first; learn the DOM! var aReferenceToMyDiv = document.getElementById('foo'); aReferenceToMyDiv.parentNode.removeChild(aReferenceToMyDiv); ... will remove the <div> element when applied to the following
这个问题在这里已经有了答案: RegEx匹配除XHTML自包含标签之外的开放标签35个答案 你想从文档中移除一个<div>元素? 首先要做的事情 学习DOM! var aReferenceToMyDiv = document.getElementById('foo'); aReferenceToMyDiv.parentNode.removeChild(aReferenceToMyDiv); ...应用于以下DOM结构时将移除<div>元素: <div id="foo"> <span>...</span> other stuff... </div>
This question already has an answer here: How do you use a variable in a regular expression? 16 answers 这是你如何从字符串建立一个新的正则表达式: var v = '[A-Za-z0-9]'; var regExp = new RegExp('^(' + v + '){1,8}$'); console.log(regExp);
这个问题在这里已经有了答案: 你如何在正则表达式中使用变量? 16个答案 这是你如何从字符串建立一个新的正则表达式: var v = '[A-Za-z0-9]'; var regExp = new RegExp('^(' + v + '){1,8}$'); console.log(regExp);
Possible Duplicate: How do you pass a variable to a Regular Expression JavaScript? javascript regular expressions with variables? I want to create a function which replace text in javascript using Regular expression like function(str, source, target) { /* str = string, source = text to replace, target = text with replace */ str.replace(/source/i, target); } Now my probl
可能重复: 你如何将一个变量传递给正则表达式JavaScript? JavaScript的正则表达式与变量? 我想创建一个函数,使用正则表达式来替换javascript中的文本 function(str, source, target) { /* str = string, source = text to replace, target = text with replace */ str.replace(/source/i, target); } 现在我的问题是在这里,这个函数将source视为文本而不是变量。 我怎样才能解决这个问题? 您
This question already has an answer here: How do you use a variable in a regular expression? 16 answers 使用正则表达式对象并试试这个: var modifiers = "gi" var patt = new RegExp(term,modifiers); var textSplit = text.split(patt); var term = /TO/gi; var textSplit = text.split(term);
这个问题在这里已经有了答案: 你如何在正则表达式中使用变量? 16个答案 使用正则表达式对象并试试这个: var modifiers = "gi" var patt = new RegExp(term,modifiers); var textSplit = text.split(patt); var term = /TO/gi; var textSplit = text.split(term);
This question already has an answer here: passing variable to a regexp in javascript [duplicate] 4 answers How do you use a variable in a regular expression? 16 answers var args = ['Name', 'Surname', 'City', 'Dog']; var regExp = new RegExp(args.join("|")); if (regExp.test(names)) { alert('true'); } More generally, you just need to concatenate the arguments as a string inside a new RegEx
这个问题在这里已经有了答案: 将变量传递给javascript中的正则表达式[复制] 4个回答 你如何在正则表达式中使用变量? 16个答案 var args = ['Name', 'Surname', 'City', 'Dog']; var regExp = new RegExp(args.join("|")); if (regExp.test(names)) { alert('true'); } 更一般地说,你只需要将参数连接成一个新的RegExp对象中的字符串。 根据变量中的内容,您可能需要首先将它们转义 - 但StackOverflow中有足够的位
This question already has an answer here: How do you use a variable in a regular expression? 16 answers You should create a specific Regular Expression object to use with the .match() function. This way you can create your regex with a string and insert the variable when creating it: var changing_value = "put"; var re = new RegExp("\b\w*" + changing_value + "\w*\b", "ig"); Note that the i
这个问题在这里已经有了答案: 你如何在正则表达式中使用变量? 16个答案 您应该创建一个特定的正则表达式对象以与.match()函数一起使用。 这样你就可以用一个字符串创建你的正则表达式,并在创建它时插入该变量: var changing_value = "put"; var re = new RegExp("\b\w*" + changing_value + "\w*\b", "ig"); 请注意,忽略大小写( i )和全局( g )修饰符被指定为RegExp构造函数的第二个参数,而不是实际表达式的一
This question already has an answer here: How do you use a variable in a regular expression? 16 answers RegExp is your friend: var p = "\*\|", s = "\|\*" var reg = new RegExp(p + '(\d{3,})' + s, 'g') "*|1387461375|* hello *|sfa|* *|3135145|* test".match(reg) Working example. You can construct a RegExp object using your variables first. Also remember to escape * and | while forming Re
这个问题在这里已经有了答案: 你如何在正则表达式中使用变量? 16个答案 RegExp是你的朋友: var p = "\*\|", s = "\|\*" var reg = new RegExp(p + '(\d{3,})' + s, 'g') "*|1387461375|* hello *|sfa|* *|3135145|* test".match(reg) 工作示例。 您可以先使用变量构造一个RegExp对象。 还要记住要逃避*和| 同时形成RegExp对象: var p = "*|"; var s = "|*"; var re = new RegExp(p.replace(/([*|])/g, '\$1')
This question already has an answer here: How do you use a variable in a regular expression? 16 answers Use the RegExp constructor instead of a RegExp literal. The constructor takes a string as its first param, so you can build it any way you like. function remove_class(div, klass) { div.className = div.className.replace( new RegExp('(?:^|\s)' + klass + '(?!\S)'), '' ); } Notice that
这个问题在这里已经有了答案: 你如何在正则表达式中使用变量? 16个答案 使用RegExp构造函数而不是RegExp文字。 构造函数接受一个字符串作为它的第一个参数,所以你可以用你喜欢的方式来构建它。 function remove_class(div, klass) { div.className = div.className.replace( new RegExp('(?:^|\s)' + klass + '(?!\S)'), '' ); } 请注意,使用构造函数时不需要分隔符(前导斜杠和尾随前斜杠)。 相反使用/ regexp
Possible Duplicate: How do you pass a variable to a Regular Expression JavaScript? I need help to resolve the issue because the code fails due to numLength[0]: <html> <body> <script type="text/javascript"> function xyz(){ var predefLength = "5,5"; var number = "20"; var numLength = predefLength.split(","); var result = /^[-+]?d{0,numLength[0]}(.d{0,numLength[1]})?$/.test(n
可能重复: 你如何将一个变量传递给正则表达式JavaScript? 我需要帮助来解决问题,因为代码由于numLength [0]而失败: <html> <body> <script type="text/javascript"> function xyz(){ var predefLength = "5,5"; var number = "20"; var numLength = predefLength.split(","); var result = /^[-+]?d{0,numLength[0]}(.d{0,numLength[1]})?$/.test(number); document.write(result); } </script>