提取字符串中的数组。

huangapple go评论60阅读模式
英文:

Extract the array out of the string

问题

Expected:

['Biller.Customer.Data@Taxonomy', 'Product.Platform and Enterprise Services Data.Data@Taxonomy']

I tried using regex and it did nothing:

string.replace(/"/g, '')

Thank you in advance.

英文:

How do I extract the array out of the string?

Current:

"['Biller.Customer.Data@Taxonomy', 'Product.Platform and Enterprise Services Data.Data@Taxonomy']"

Expected:

['Biller.Customer.Data@Taxonomy', 'Product.Platform and Enterprise Services Data.Data@Taxonomy']

I tried using regex and it did nothing:

string.replace(/"/g, '')

Thank you in advance.

答案1

得分: 4

更好的做法是生成一个有效的 JSON 字符串,而不是那种格式。然而,如果你因为某种无法控制的因素而被困在这种格式中,那么你可以使用或编写一个能够处理它的解析器(例如 JSON5):

TS Playground

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->
<script type="module">
import JSON5 from "https://cdn.jsdelivr.net/npm/json5@2.2.3/dist/index.min.mjs";
const input = "['Biller.Customer.Data@Taxonomy', 'Product.Platform and Enterprise Services Data.Data@Taxonomy']";
const array = JSON5.parse(input);
console.log(array); // ["Biller.Customer.Data@Taxonomy", "Product.Platform and Enterprise Services Data.Data@Taxonomy"]
</script>
<!-- end snippet -->

或者,如果你理解并接受字符串评估的风险,也可以使用 eval()

TS Playground

const input = "['Biller.Customer.Data@Taxonomy', 'Product.Platform and Enterprise Services Data.Data@Taxonomy']";
const array = eval(input);
console.log(array); // ["Biller.Customer.Data@Taxonomy", "Product.Platform and Enterprise Services Data.Data@Taxonomy"]
英文:

It will be better to produce a valid JSON string instead of that format. However, if you're stuck with that because of a factor out of your control, then you can use or write a parser that can handle it (e.g. JSON5):

TS Playground

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;script type=&quot;module&quot;&gt;

import JSON5 from &quot;https://cdn.jsdelivr.net/npm/json5@2.2.3/dist/index.min.mjs&quot;;

const input = &quot;[&#39;Biller.Customer.Data@Taxonomy&#39;, &#39;Product.Platform and Enterprise Services Data.Data@Taxonomy&#39;]&quot;;

const array = JSON5.parse(input);

console.log(array); // [&quot;Biller.Customer.Data@Taxonomy&quot;, &quot;Product.Platform and Enterprise Services Data.Data@Taxonomy&quot;]

&lt;/script&gt;

<!-- end snippet -->

Or, if you understand and accept the dangers of string evaluation, you can also use eval():

TS Playground

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const input = &quot;[&#39;Biller.Customer.Data@Taxonomy&#39;, &#39;Product.Platform and Enterprise Services Data.Data@Taxonomy&#39;]&quot;;

const array = eval(input);

console.log(array); // [&quot;Biller.Customer.Data@Taxonomy&quot;, &quot;Product.Platform and Enterprise Services Data.Data@Taxonomy&quot;]

<!-- end snippet -->

答案2

得分: 0

你可以使用 JSON.parse,但请注意,单引号在 JSON 中不是有效的字符串分隔符,如果你不是生成字符串的人,请使用 replaceAll 将它们更改为双引号。如果你可以控制原始字符串,只需使用双引号作为分隔符。

JSON.parse(yourString.replaceAll("&quot;&#39;&quot;", "&quot;&quot;")) 可以解决你的问题,但这非常危险,因为如果你的字符串中有一个撇号,那将会导致无效的 JSON,因此 JSON.parse 会引发错误。

注意:非常仔细地关注 replaceAll 中的单引号和双引号。

英文:

You could use JSON.parse, but note that single quotes are not valid string delimiters in JSON, use replaceAll to change them to double quotes if you are not the one generating the string. If you are in control of the original string, just use double quotes as delimiters instead.

JSON.parse(yourString.replaceAll(&quot;&#39;&quot;, &#39;&quot;&#39;)) could solve your problem, but it is very risky, since if one of your strings had an apostrophe, that would become invalid json, thus JSON.parse would throw an error.

Note: Pay VERY close attention to the single and double quotes in the replaceAll

答案3

得分: 0

以下是翻译好的部分:

myarray=(this.stringArray.match(/&#39;([^,])*&#39;/g) ||[])
.map(x=>x.slice(0,-1).slice(1))

英文:
myarray=(this.stringArray.match(/\&#39;([^,])*\&#39;/g) ||[])
          .map(x=&gt;x.slice(0,-1).slice(1))

答案4

得分: -2

EDIT

如果你只想将任何字符串拆分成数组,那么你可以根据你的 TypeScript 版本使用以下方法:

  1. [...yourString]
  2. yourString.split('separator')
  3. Array.from(yourString)

就像 Gustavodevjoco 在评论中所述,第1和第3种方法会将你的字符串拆分成每个字符。
第2种方法可以部分实现你的目标,但你会受制于括号,并且需要像这样使用它:

yourString.split(', ')

因此,逗号后面的空格不会出现在结果数组中。

但是如果你正在处理表示数据的类似 JSON 的字符串,那么使用 jsejcksn 描述的方法将是最佳选择。

英文:

EDIT

If you only want to split any string into an array then you can use the following methods depending on your version of TypeScript:

  1. [...yourString]
  2. yourString.split(&#39;separator&#39;)
  3. Array.from(yourString)

Like in the comments by Gustavo and devjoco described, No1 and No3 would split your string into each characters.
No2 would partially achieve your goal, but you would be stuck with the enclosing brackets and would e.g. need to use it like this:

yourString.split(&#39;, &#39;)

So the whitespace after the comma would not end up in the result array.

BUT if you are working with JSON-like strings representing data, then using the approach jsejcksn described would be your best bet.

huangapple
  • 本文由 发表于 2023年6月8日 02:54:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定