两个特殊字符之间的正则表达式 [英] regex between two special characters

查看:107
本文介绍了两个特殊字符之间的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在JQuery或Javascript中复制此PHP代码段...

Is there a way to replicate this PHP snippet in JQuery or Javascript...

<?php 
  $A = preg_match_all('#{.*(.*)}#U', $value, $B); //matches all between {} put it into array "B"
  $C = $B[1]; // array[0] = "{content}" --- array[1] = "content"
?>

4个小时左右,我一直试图找到类似的东西,但无济于事. 基本上,目标是找到{}之间的所有内容,然后将该信息提取出来才有用.

I have been trying to find something similar for like 4 hours or so to no avail. The goal is basically to find everything between { and } and then pull that information out to be useful.

我不是Javascript专家,所以我将不胜感激.预先谢谢你.

I am no Javascript expert so I would appreciate any help. Thank you in advance.

推荐答案

您可以使用以下方法将{和}之间的所有文本块放入数组:

You can get all blocks of text between { and } into an array with this:

function getBracedText(input) {
    var re = /\{(.*?)\}/g, matches, output = [];
    while (matches = re.exec(input)) {
        output.push(matches[1]);
    }
    return(output);
}

var str = "Some text {tag} and more {text}";
var results = getBracedText(str);
// results == ["tag", "text"];

正在运行的演示: http://jsfiddle.net/jfriend00/DT4Km/

这篇关于两个特殊字符之间的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆