简体   繁体   English

Javascript正则表达式仅替换第一个匹配项

[英]Javascript Regex only replacing first match occurence

I am using regular expressions to do some basic converting of wiki markup code into copy-pastable plain text, and I'm using javascript to do the work. 我正在使用正则表达式对Wiki标记代码进行一些基本的转换,以将其转换为可复制粘贴的纯文本,并且正在使用javascript来完成这项工作。

However, javascript's regex engine behaves much differently to the ones I've used previously as well as the regex in Notepad++ that I use on a daily basis. 但是,javascript的regex引擎的行为与我以前使用的以及日常使用的Notepad ++中的regex完全不同。

For example- given a test string: 例如-给定一个测试字符串:

==Section Header==
===Subsection 1===
# Content begins here.
## Content continues here.

I want to end up with: 我要结束于:

Section Header
Subsection 1
# Content begins here.
## Content continues here.

Simply remove all equals signs. 只需删除所有等号。

I began with the regex setup of: 我从正则表达式设置开始:

var reg_titles = /(^)(=+)(.+)(=+)/

This regex searches for lines that begin with one or more equals with another set of one or more equals. 此正则表达式搜索以一个或多个等于开始于另一组一个或多个等于的行。 Rubular shows that it matches my lines accurately and does not catch equals signs in the middle of contet. Rubular显示它与我的线条精确匹配,并且在竞争中没有捕捉到等号。 http://www.rubular.com/r/46PrkPx8OB http://www.rubular.com/r/46PrkPx8OB

The code to replace the string based on regex 基于正则表达式替换字符串的代码

var lines = $('.tb_in').val().split('\n'); //use jquery to grab text in a textarea, and split into an array of lines based on the \n 
for(var i = 0;i < lines.length;i++){
  line_temp = lines[i].replace(reg_titles, "");
  lines[i] = line_temp; //replace line with temp
}
$('.tb_out').val(lines.join("\n")); //rejoin and print result

My result is unfortunately: 我的结果很不幸:

Section Header==
Subsection 1===
# Content begins here.
## Content continues here.

I cannot figure out why the regex replace function, when it finds multiple matches, seems to only replace the first instance it finds, not all instances. 我不知道为什么当找到多个匹配项时,正则表达式替换函数似乎只替换它找到的第一个实例,而不是全部实例。

Even when my regex is updated to: var reg_titles = /(={2,})/ 即使我的正则表达式更新为:var reg_titles = /(== {2,})/

"Find any two or more equals", the output is still identical. “找到任何两个或更多个相等”,输出仍然相同。 It makes a single replacement and ignores all other matches. 它进行一次替换,并忽略所有其他匹配项。

No one regex expression executor behaves this way for me. 没有人正则表达式执行器对我有这种行为。 Running the same replace multiple times has no effect. 多次运行相同的替换无效。

Any advice on how to get my string replace function to replace ALL instances of the matched regex instead of just the first one? 关于如何获取我的字符串替换函数以替换匹配的正则表达式的所有实例而不只是第一个实例的任何建议?

^=+|=+$

You can use this.Do not forget to add g and m flags.Replace by ``.See demo. 您可以使用它,别忘了添加gm标志,用``代替。

http://regex101.com/r/nA6hN9/28 http://regex101.com/r/nA6hN9/28

添加g修饰符以进行全局搜索:

var reg_titles = /^(=+)(.+?)(=+)/g

Your regex is needlessly complex, and yet doesn't actually accomplish what you set out to do. 您的正则表达式不必要地复杂,但实际上并没有完成您打算要做的事情。 :) You might try something like this instead: :)您可以尝试这样的方法:

var reg_titles = /^=+(.+?)=+$/;
lines = $('.tb_in').val().split('\n');
lines.forEach(function(v, i, a) {
    a[i] = v.replace(reg_titles, '$1');
})
$('.tb_out').val(lines.join("\n"));

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

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