我正试图从一个可能是csv或tab分隔的文件中分割文本。将split函数与","
一起使用适用于csv文件,但当尝试并传递这样的正则表达式时,"/[s,;tn]+/"
会将所有文本聚为一个连续字符串。
是否有一个正则表达式可以拆分字符串,无论文本是用逗号还是制表符分隔?文件将仅以csv或tab分隔。
正则表达式/[,t]/gm
将在字符串中找到任何逗号或制表符。您可以在split方法中使用它来拆分字符串。正则表达式还为s
和;
查找空格,为n
查找换行符(您没有编写要拆分的内容(。在这里,您可以看到我如何使用这个正则表达式的示例。
const stringToSplit =`RegExr was created by gskinner.com, and is proudly hosted by Media Temple.
Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & JavaScript flavors of RegEx are supported. Validate your expression with Tests mode.
The side bar includes a Cheatsheet, full Reference, and Help. You can also Save & Share with the Community, and view patterns you create or favorite in My Patterns.
Explore results with the Tools below. Replace & List output custom results. Details lists capture groups. Explain describes your expression in plain English.`
const re = /[,t]/gm;
const splitted = stringToSplit.split(re);
console.log(splitted)