I want to replace a markdown image pattern with the given filename in the textarea with an empty string.
我想用textarea中的给定文件名替换带有空字符串的markdown图像模式。
So the pattern is ![alt](http://somehost/uploads/filename.jpg)
所以模式是![alt](http://somehost/uploads/filename.jpg)
This is the code I have now:
这是我现在的代码:
var content = target.val();
var fileName = someDynamicValue;
var regex = new RegExp(RegExp.escape('![') + '.*' + RegExp.escape(']') + RegExp.escape('(') + '.*' + RegExp.escape(fileName) + RegExp.escape(')'), 'i');
var found = regex.exec(content);
var newContent = content.replace(regex, "");
target.val(newContent);
RegExp.escape= function(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
};
For example var fileName = filename.jpg
. Then I need to match ![alt](http://somehost/uploads/filename.jpg)
and replace it with an empty string.
例如var fileName = filename.jpg。然后我需要匹配![alt](http://somehost/uploads/filename.jpg)并用空字符串替换它。
Everything works great if the content
includes one image. But if there are more then one, for example:
如果内容包含一个图像,一切都很好。但如果有更多,例如:
Some text
![alt](http://somehost/uploads/filename.jpg)
some text![alt2](http://somehost/uploads/filename2.jpg)
more text.一些文字![alt](http://somehost/uploads/filename.jpg)一些文字![alt2](http://somehost/uploads/filename2.jpg)更多文字。
then var found
includes ![alt](http://somehost/uploads/filename.jpg)![alt2](http://somehost/uploads/filename2.jpg)
, but I need to match only ![alt](http://somehost/uploads/filename.jpg)
.
var找到包括![alt](http://somehost/uploads/filename.jpg)![alt2](http://somehost/uploads/filename2.jpg),但我只需匹配![alt]( HTTP://somehost/uploads/filename.jpg)。
What regex I need in this case?
在这种情况下我需要什么正则表达式?
2 个解决方案
#1
0
Not sure how you are trying to put the strings together but'.*'
is greedily matching up to the last filename.
So, it should probably be '.*?'
.
不确定你是如何尝试将字符串放在一起但'。*'贪婪地匹配到最后的文件名。所以,应该是'。*?'。
However, if the filenames are different then it shouldn't have matched.
Another thing is you should in general stop it from running past the next [alt]
with
something like '[^\[\]]*'
但是,如果文件名不同,则它不应该匹配。另一件事是你通常应该阻止它跑过下一个[alt],比如'[^ \ [\]] *'
Edit:
RegExp.escape('![') + '.*'
+ RegExp.escape(']') + RegExp.escape('(') + '.*' + RegExp.escape(fileName) + RegExp.escape(')'), 'i');
RegExp.escape('![')+'。*'+ RegExp.escape(']')+ RegExp.escape('(')+'。*'+ RegExp.escape(fileName)+ RegExp.escape(' )'), '一世');
is the culprit.
是罪魁祸首。
Try
RegExp.escape('![') + '[^\]]*' + RegExp.escape(']') + RegExp.escape('(') + '[^\[\]]*?' + RegExp.escape(fileName) + RegExp.escape(')'), 'i');
RegExp.escape('![')+'[^ \]] *'+ RegExp.escape(']')+ RegExp.escape('(')+'[^ \ [\]] *?'+ RegExp .escape(fileName)+ RegExp.escape(')'),'i');
#2
1
Use non-greedy quantifiers will do:
使用非贪婪量词会做:
!\[(.*?)\]\((.*?)\)
You can check it out online: https://regex101.com/r/kfi8qI
您可以在线查看:https://regex101.com/r/kfi8qI
#1
0
Not sure how you are trying to put the strings together but'.*'
is greedily matching up to the last filename.
So, it should probably be '.*?'
.
不确定你是如何尝试将字符串放在一起但'。*'贪婪地匹配到最后的文件名。所以,应该是'。*?'。
However, if the filenames are different then it shouldn't have matched.
Another thing is you should in general stop it from running past the next [alt]
with
something like '[^\[\]]*'
但是,如果文件名不同,则它不应该匹配。另一件事是你通常应该阻止它跑过下一个[alt],比如'[^ \ [\]] *'
Edit:
RegExp.escape('![') + '.*'
+ RegExp.escape(']') + RegExp.escape('(') + '.*' + RegExp.escape(fileName) + RegExp.escape(')'), 'i');
RegExp.escape('![')+'。*'+ RegExp.escape(']')+ RegExp.escape('(')+'。*'+ RegExp.escape(fileName)+ RegExp.escape(' )'), '一世');
is the culprit.
是罪魁祸首。
Try
RegExp.escape('![') + '[^\]]*' + RegExp.escape(']') + RegExp.escape('(') + '[^\[\]]*?' + RegExp.escape(fileName) + RegExp.escape(')'), 'i');
RegExp.escape('![')+'[^ \]] *'+ RegExp.escape(']')+ RegExp.escape('(')+'[^ \ [\]] *?'+ RegExp .escape(fileName)+ RegExp.escape(')'),'i');
#2
1
Use non-greedy quantifiers will do:
使用非贪婪量词会做:
!\[(.*?)\]\((.*?)\)
You can check it out online: https://regex101.com/r/kfi8qI
您可以在线查看:https://regex101.com/r/kfi8qI