为javascript添加/删除文本多行

我有需要删除/添加的多行字符串,这里是我想编辑的数据


  data{

      id

      date

      ***progress{

        update

        progressStatus

      }***

      events {

        id

        time

      }

    }

我的观点是如何删除


progress{

            update

            progressStatus

          }

我试过“替换”,“分配”将其删除,如下所示,但不起作用


  const test = data.replace(progress, '');

谢谢你。


桃花长相依
浏览 209回答 3
3回答

神不在的星期二

这是逻辑,我正在考虑您要删除具有开放大括号('{')和结束大括号('}')的“进度”:var data = `data{&nbsp; &nbsp; &nbsp; id&nbsp; &nbsp; &nbsp; date&nbsp; &nbsp; &nbsp; ***progress{&nbsp; &nbsp; &nbsp; &nbsp; update&nbsp; &nbsp; &nbsp; &nbsp; progressStatus&nbsp; &nbsp; &nbsp; }***&nbsp; &nbsp; &nbsp; events {&nbsp; &nbsp; &nbsp; &nbsp; id&nbsp; &nbsp; &nbsp; &nbsp; time&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }`;function _remove(data, key) {&nbsp; var s = data.indexOf(key);&nbsp; var e = ((s) => {&nbsp; &nbsp; for(var i=s; i<data.length; i++){&nbsp; &nbsp; &nbsp; if(data[i] == "}")&nbsp; &nbsp; &nbsp; &nbsp; return i;&nbsp; &nbsp; }&nbsp; })(s + key.length);&nbsp; console.log(data.replace(data.substr(s, e-s+1), ""));}_remove(data, 'progress');也可以用正则表达式来完成!

慕容708150

您可以尝试使用正则表达式const regex = /progress{[^}]+}\s+/gm;const str = `data{&nbsp; &nbsp; &nbsp; id&nbsp; &nbsp; &nbsp; date&nbsp; &nbsp; &nbsp; progress{&nbsp; &nbsp; &nbsp; &nbsp; update&nbsp; &nbsp; &nbsp; &nbsp; progressStatus&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; events {&nbsp; &nbsp; &nbsp; &nbsp; id&nbsp; &nbsp; &nbsp; &nbsp; time&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }`;const subst = ``;// The substituted value will be contained in the result variableconst result = str.replace(regex, subst);console.log('Substitution result: ', result);

猛跑小猪

什么是我有多条线?grapqh 的查询会返回一个对象,为什么还是一个多字符串?在您的情况下(查询数据是字符串),请尝试:const data = ` data{&nbsp; &nbsp; &nbsp; id&nbsp; &nbsp; &nbsp; date&nbsp; &nbsp; &nbsp; progress{&nbsp; &nbsp; &nbsp; &nbsp; update&nbsp; &nbsp; &nbsp; &nbsp; progressStatus&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; events {&nbsp; &nbsp; &nbsp; &nbsp; id&nbsp; &nbsp; &nbsp; &nbsp; time&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }`const newData = data.replace(`progress{&nbsp; &nbsp; &nbsp; &nbsp; update&nbsp; &nbsp; &nbsp; &nbsp; progressStatus&nbsp; &nbsp; &nbsp; }`, '')console.log(newData)如果你想删除对象中的一个键,试试这个:delete data.progress;// or,delete data['progress'];// or,const prop = "progress";delete data[prop];演示:const data = {&nbsp; &nbsp; &nbsp; &nbsp;progress: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x: "x"&nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; &nbsp;events: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: 1&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }delete data.progressconsole.log(data)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript