简体   繁体   English

使用正则表达式捕获`\\“`并拆分成数组

[英]Using regex to capture `\"` and split into array

I have come across something that I haven't seen before and google is no use because it keeps removing \\" from my search. 我接触过的东西,我以前从未见过的和谷歌是没有用的,因为它使删除\\"从我的搜索。

First of all, what does \\" mean? I am accessing an API and for one of the key's values it is a string of tags, but then if one of the tags has two words it uses \\" to show that the tag is two words. 首先,是什么\\"是什么意思?我访问的API,并在关键的价值观之一,它是标记的字符串,但随后如果标签之一有两个词,它使用\\" ,以表明该标签是二话。 Example below 以下示例

"foo bar baz \\"taz foo\\" goo too loo"

I am trying to split those up into an array like so ["foo", "bar", "baz", "taz foo", "goo", "too", "loo"] 我试图把它们分成像这样的数组["foo", "bar", "baz", "taz foo", "goo", "too", "loo"]

However I am having one hell of a time figuring out how to capture the \\" and making it so I can split them to an array like above. 然而,我有一个地狱的时间来弄清楚如何捕获\\"并制作它,以便我可以将它们分成如上所述的数组。

Use String.prototype. 使用String.prototype。 replace() for fun and profit: replace()获取乐趣和利润:

var s = "foo bar baz \"taz foo\" goo too loo";

var arr = [];
s.replace(/[^\s"]+|"[^"]*"/g, function($1) {
  arr.push($1.replace(/"/g,''));
});

console.log(arr); // ["foo", "bar", "baz", "taz foo", "goo", "too", "loo"]

Create an empty array arr to push matches into 创建一个空数组arr以将匹配推送到
Use replace to collect $1 matches: anything that is 使用replace来收集$1匹配:任何东西
[^\\s"]+ not a whitespace or doubleQuote (one or infinite times) [^\\s"]+不是空格或双引号(一次或无限次)
| or 要么
"[^"]*" anything between " " that is not a quote [^"] (zero or infinite times * ) "[^"]*"之间的任何东西" "不是引用[^"] (零或无限次*

\\" means to escape the double quote character. It's called an escape character . \\"意味着逃避双引号字符。它被称为转义字符

If you want to use a double inside a string that is double quoted, you will have to use the escape character. 如果要在双引号的字符串中使用double,则必须使用转义字符。

Say for example you want to store a string abcd"xyz"abcd 比如说你想存储一个字符串abcd"xyz"abcd

console.log("abcd"xyz"abcd");

will give you an error because double quotes before xyz mark the end of string and quote after xyz mark the beginning of new string, hence an error occurs because of improper syntax. 会给你一个错误,因为在xyz之前的双引号标记字符串的结尾并且在xyz标记新字符串的开头之后引用,因此由于语法不正确而发生错误。

Therefore to have double quotes inside a string you will have to 因此,要在字符串中使用双引号,您必须这样做

either escape the double quotes using \\" 使用\\"转义双引号

console.log("abcd\"xyz\"abcd");

or put double quotes inside a single quoted string 或将双引号放在单引号字符串中

console.log('abcd"xyz"abcd');

As for your splitting question, you can split on a regex to get that array like this: 至于你的分裂问题,你可以拆分正则表达式来获得这样的数组:

> "foo bar baz \"taz foo\" goo too loo".match(/\w+|(?:")[^"]*?(?:")/g);

["foo", "bar", "baz", ""taz foo"", "goo", "too", "loo"]

and then you can iterate over the array and remove double quotes like this: 然后你可以迭代数组并删除双引号,如下所示:

> '"abcd xyz"'.replace(/"/g, '');
"abcd xyz"

Combined code: 合并代码:

> arr="foo bar baz \"taz foo\" goo too loo".match(/\w+|"[^"]*?(?=")/g);
["foo", "bar", "baz", ""taz foo", "goo", "too", "loo"]
> for(var i=0;i<arr.length;i++){arr[i]=arr[i].replace(/"/g, '');}
> console.log(arr);
["foo", "bar", "baz", "taz foo", "goo", "too", "loo"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM