Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于替换文本箭头的Javascript正则表达式-><-_Javascript_Regex_Replace_Match - Fatal编程技术网

用于替换文本箭头的Javascript正则表达式-><-

用于替换文本箭头的Javascript正则表达式-><-,javascript,regex,replace,match,Javascript,Regex,Replace,Match,下面是我尝试做的一个例子: This is a paragraph of text. <-This is some text to be left aligned<- This is some more text. This is a paragraph of text. ->This is some text to be centered<- This is some more text. This is a p

下面是我尝试做的一个例子:

This is a paragraph of text.  
<-This is some text to be left aligned<-
This is some more text.

This is a paragraph of text.  
                       ->This is some text to be centered<-
This is some more text.

This is a paragraph of text.  
                                        ->This is some text to be right aligned->
This is some more text.
这是一段文字。
这是更多的文本。
箭头字符用于指定(由用户)要对齐文本的方式。以下是我到目前为止一直在整理的内容:

var text = "->This is some text to be centered<-";
var center_text = text.match("->(.*)<-");
if(center_text){
    text = '<span style="text-align:center;">'+center_text[1]+'</span>';
    console.log(text);
}

var text=“->这是一些要居中的文本(.*)这是右对齐的->。这是一个段落。->这是居中对齐的为什么要调用
String#match
?您可以在单个
String#replace
调用:

var text = "->Text<- ->Text2<-";
var repl = text.replace(/->(.*?)<-/g, "<center>$1</center>");
//=> "<center>Text</center> <center>Text2</center>"

var text=“->textext2因为中间对齐块、左对齐块和右对齐块使用相同的分隔符,因此尝试一次只匹配一个类型块的策略存在过度匹配的风险。考虑这个例子:

-> left ->
<- right <-
->左->

是的,将贪婪的
*
更改为懒惰的
*?

var center_text = text.match("->(.*?)<-");

var-center\u text=text.match(“->”(.*)(?:(?!感谢您的回答,如果它没有执行,我会告诉您,但它看起来工作得很好!如果
另一件事:
body='this';var repl=body.replace(//g,$1'),使用非贪婪(
*?
)更接近所需的输出
I get:
这是测试
。使用'body='this';var repl=body.replace(//g,$1');'I get:
这是>
body.replace(//g,$1');
将为您提供
这是测试
-> left ->
<- right <-
<center> left ->
</center> right <-
var result = 
    text.replace(
        /(<-|->)(.*?)(<-|->)/g,
        function(m, l, c, r) {
            var cls = "";
            if (l == "<-" && r == "<-") {
                cls = "align-left";
            } else if (l == "->" && r == "->") {
                cls = "align-right";
            } else if (l == "->" && r == "<-") {
                cls = "align-center";
            } else if (l == "<-" && r == "->") {
                cls = "align-justify";
            }

            return '<div class="' + cls + '">' + c + '</div>';
        });
var center_text = text.match("->(.*?)<-");
var center_text = text.match("->((?:(?!<-).)*)<-");
var regexc = /->(.*?)<-/g;  // Regex for centre
var regexl = /<-(.*?)<-/g;  // Regex for left
var regexr = /->(.*?)->/g;  // Regex for right
var replacec = "<span style=\"text-align:center;\">'$1'</span>"; // Centre replacement
var replacel = "<span style=\"text-align:left;\">'$1'</span>";   // Left replacement
var replacer = "<span style=\"text-align:right;\">'$1'</span>";  // Right replacement

var results = text.replace(regexc, replacec).replace(regexl, replacel).replace(regexr, replacer);