[英]how to remove white space at the beginning and end of a regular expression?
I create a javascript parser for my markup language. 我为标记语言创建了一个JavaScript解析器。 I wish no spaces between tags and content like this:
我希望标签和内容之间没有空格,如下所示:
__underline a sentence __and more.
(or correctly : __underline a sentence__ and more.)
to : 至 :
<u>underline a sentence</u> and more.
But result is in the first case : 但是结果在第一种情况下:
<u>underline a sentence </u>and more.
My code : 我的代码:
var tabML = ['\'','*','_'],
tabHTML = ['em','strong','u']
tag, char;
for(var i=0; i<tabML.length; i++){
tag = tabHTML[i]; char = tabML[i];
regex = new RegExp(char+char+'(.+)'+char+char, 'ig');
txt = txt.replace(regex, '<'+tag+'>$1</'+tag+'>');
}
Thanks. 谢谢。
Use the below regex and then replace the matched characters with <u>$1</u>
使用以下正则表达式,然后将匹配的字符替换为
<u>$1</u>
__\s*(.*?)\s*__\s*
> var s1 = "__underline a sentence __and more."
undefined
> var s2 = "__underline a sentence__ and more."
undefined
> s1.replace(/__\s*(.*?)\s*__\s*/g, '<u>$1</u> ')
'<u>underline a sentence</u> and more.'
> s2.replace(/__\s*(.*?)\s*__\s*/g, '<u>$1</u> ')
'<u>underline a sentence</u> and more.'
__(.*?)[ ]*__[ ]*
Try this.Replace by <u>$1</u>
.See demo. 试试这个,用
<u>$1</u>
代替。
https://regex101.com/r/tX2bH4/5 https://regex101.com/r/tX2bH4/5
var re = /__(.*?)[ ]*__[ ]*/gm;
var str = '__underline a sentence __and more.\n__underline a sentence__ and more.';
var subst = '<u>$1</u> ';
var result = str.replace(re, subst);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.