英文:
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):
<!-- 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()
:
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):
<!-- 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 -->
Or, if you understand and accept the dangers of string evaluation, you can also use eval()
:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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"]
<!-- end snippet -->
答案2
得分: 0
你可以使用 JSON.parse
,但请注意,单引号在 JSON 中不是有效的字符串分隔符,如果你不是生成字符串的人,请使用 replaceAll
将它们更改为双引号。如果你可以控制原始字符串,只需使用双引号作为分隔符。
JSON.parse(yourString.replaceAll(""'"", """"))
可以解决你的问题,但这非常危险,因为如果你的字符串中有一个撇号,那将会导致无效的 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("'", '"'))
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(/'([^,])*'/g) ||[])
.map(x=>x.slice(0,-1).slice(1))
英文:
myarray=(this.stringArray.match(/\'([^,])*\'/g) ||[])
.map(x=>x.slice(0,-1).slice(1))
答案4
得分: -2
EDIT
如果你只想将任何字符串拆分成数组,那么你可以根据你的 TypeScript 版本使用以下方法:
[...yourString]
yourString.split('separator')
Array.from(yourString)
就像 Gustavo 和 devjoco 在评论中所述,第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:
[...yourString]
yourString.split('separator')
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(', ')
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论