我目前正在尝试解析一个 smil (xml) 文件。
我遇到的最大问题是该行是否只包含空格和行尾。
我正在尝试:
if(line.trim()==='\n')
if(line.trim().length<='\n'.length)
n='\n';
if(line.trim()===n)
它们都不起作用。 有没有办法检查字符串中是否没有“真实”字符?或者如果字符串仅包含\t、\n 和空格?
最佳答案
阅读一些关于正则表达式的教程,然后试试这个
if (/^\s*$/.test(line)) console.log('line is blank');
/^\s*$/
是一个正则表达式,意思是
^ anchor begin of string
\s whitespace character class (space, tab, newline)
* zero or more times
$ end of string
关于javascript - 在 js/node.js 中检查一行是否只包含空格和\n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18724378/