Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text to JSON #99

Open
xgqfrms opened this issue Jul 11, 2020 · 2 comments
Open

text to JSON #99

xgqfrms opened this issue Jul 11, 2020 · 2 comments
Labels
js relpace \n js relpace \n regex regex text to JSON text to JSON

Comments

@xgqfrms
Copy link
Owner

xgqfrms commented Jul 11, 2020

text to JSON

https://www.cnblogs.com/xgqfrms/p/13283680.html

https://repl.it/@xgqfrms/js-relpace-n#readme.md

https://www.cloudflare.com/cdn-cgi/trace

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2020-07-01
 * @modified
 *
 * @description text to JSON & js replace \n
 * @augments
 * @example
 * @link https://www.cnblogs.com/xgqfrms/p/13283680.html
 * @link https://repl.it/@xgqfrms/js-relpace-n#readme.md
 * @link https://www.cloudflare.com/cdn-cgi/trace
 *
 */

const log = console.log;

const text = `
fl=12f405
h=www.cloudflare.com
ip=240e:e0:5a14:4e00:84de:3d45:c099:7362
ts=1594452831.277
visit_scheme=https
uag=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4198.2 Safari/537.36
colo=LAX
http=http/2
loc=CN
tls=TLSv1.3
sni=plaintext
warp=off
`;

let str = text.trim();

// log(`str`, str)

// js replace \n ???
// str = str.replace(/\n/ig, `🚀`);
// str = str.replace(/\r\n/ig, `🚀`);
str = str.replace(/(?:\r\n|\r|\n)/ig, `🚀`);
// str = str.replace(/(?:\r\n|\r|\n)/ig, `XYZ`);

// log(`str`, str, typeof str)

let arr = str.split(`🚀`);
// arr = str.split(`🚀`).join();
// arr = str.split(`=`);

// log(`arr 2`, arr)

const obj = {};

let result = arr.map((item, i) => {
  // log(`item`, item);
  // return item.trim().replace(/\\n/ig, ``);
  const [k, v] = item.trim().split(`=`);
  log(`k, v`, k, v);
  obj[k] = v;
});

// log(`result `, result)

// log(`json`, obj);
log(`json`, JSON.stringify(obj, null, 4));
log(`json`, JSON.parse(JSON.stringify(obj, null, 4)));

js replace \n

// 1. regex
str = str.replace(/(?:\r\n|\r|\n)/g, '<br>');

// split & join

https://stackoverflow.com/questions/784539/how-do-i-replace-all-line-breaks-in-a-string-with-br-elements

@xgqfrms
Copy link
Owner Author

xgqfrms commented Jul 11, 2020

@xgqfrms
Copy link
Owner Author

xgqfrms commented Jul 11, 2020

#52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js relpace \n js relpace \n regex regex text to JSON text to JSON
Projects
None yet
Development

No branches or pull requests

1 participant