正则表达式删除两个字符串间的字符实例

利用正则表明式去除八个字符串间的字符

JSPN示例1:

 代码如下

{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }

<user>
      <id>18752</id>
      <name>方方</screen_name>
      <name>方方</name>
      <province>44</province>
      <city>3</city>
</user>

 

譬喻删除<user>和</user>之间的字符串:

JSPN示例2:

 代码如下

{
    "name": "张三",
    "ourid": {
        "id": "1",
        "name": "李四"
    },
    "uuyr": "8"
}

(?is)(?<=<user>).*?(?=</user>)

JSPN示例3:

 

{ "people": [

{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },

{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},

{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }

]}

那简单通晓。在这几个示例中,独有一个名为people的变量,值是包涵七个条文的数组,每一种条目款项是壹人的记录,个中带闻明、姓和电子邮件地址。上边的以身作则演示怎样用括号将记录组合成贰个值。当然,能够选拔一样的语法表示七个值(种种值满含多少个记录):

{ "programmers": [

{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },

{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },

{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }

],

"authors": [

{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },

{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },

{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }

],

"musicians": [

{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },

{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }

] }

【格式应用】

将 JSON 数据赋值给变量

举个例子,能够创制五个新的 JavaScript 变量,然后将 JSON 格式的多寡字符串直接赋值给它:

var people = { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },

{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },

{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }

],

"authors": [

{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },

{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },

{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }

],

"musicians": [

{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },

{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }

] }

 

访问数据

固然看起来不显眼,不过地方的长字符串实际上只是二个数组;将那一个数组放进 JavaScript 变量之后,就能够很自在地寻访它。实际上,只需用点号表示法来表示数组元素。所以,要想拜访programmers 列表的率先个条指标姓氏,只需在 JavaScript 中动用上边那样的代码:

people.programmers[0].lastName;

当心,数组索引是从零开首的。所以,那行代码首先访谈people变量中的数据;然后移动到称为 programmers的条文,再移动到第叁个记录([0]);最终,访问lastName键的值。结果是字符串值 “McLaughlin”。

上边是运用一样变量的多少个示范。

people.authors[1].genre // Value is "fantasy"

people.musicians[3].lastName // Undefined. This refers to the fourth entry, and there isn't one

people.programmers[2].firstName // Value is "Elliotte"

选取这样的语法,能够拍卖其余 JSON 格式的数目,而不须要运用另外附加的 JavaScript 工具包或 API。

 

改 JSON 数据

正如能够用点号和括号访谈数据,也得以遵从一样的艺术轻易地修改数据:

people.musicians[1].lastName = "Rachmaninov";

在将字符串调换为 JavaScript 对象之后,就可以像那样修改换量中的数据。

 

换回字符串

道理当然是那样的,假设不可能轻易地将对象调换回本文提到的文本格式,那么具有数据修改都未曾太大的价值。在 JavaScript 中这种转移也很轻松:

String newJSONtext = people.toJSONString();

那样就行了!未来就赢得了八个方可在别的地点使用的文本字符串,比方,能够将它用作 Ajax 应用程序中的央浼字符串。

更关键的是,能够将别的JavaScript 对象转变为 JSON 文本。并不是只好管理原本用 JSON 字符串赋值的变量。为了对名为myObject的目的实行转变,只需举办一样款式的一声令下:

String myObjectInJSON = myObject.toJSONString();

 概念相比

【JSON和XML的比较】

实例相比较

用XML表示中国一部分省市数据如下:

<?xml version="1.0" encoding="utf-8"?>

<country>

    <name>中国</name>

    <province>

        <name>黑龙江</name>

     <cities>

            <city>哈尔滨</city>

            <city>大庆</city>

        </cities>

    </province>

    <province>

        <name>广东</name>

        <cities>

            <city>广州</city>

            <city>深圳</city>

            <city>珠海</city>

        </cities>

    </province>

</country>

 

用JSON表示如下:

{

{name:"中国", province:[ { name:"黑龙江", cities:{ city:["哈尔滨","大庆"] },

{name:"广东", cities:{ city:["广州","深圳","珠海"] } 

}

本文由365bet体育在线官网发布于关于计算机,转载请注明出处:正则表达式删除两个字符串间的字符实例

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。