Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a line only contain whitespace and \n in js/node.js

I m currently trying to parse a smil (xml) file.

The biggest issue I have is if the line do not contain anything else but whitespace and end of line.

I have trying:

if(line.trim()==='\n')
if(line.trim().length<='\n'.length)

n='\n';
if(line.trim()===n)

None of them worked. Is there a way to check if there s no 'real' character in a string? Or if the string contain only \t, \n and whitespace?

like image 919
DrakaSAN Avatar asked Dec 04 '22 09:12

DrakaSAN


1 Answers

read some tutorial on regex and then try this

  if (/^\s*$/.test(line)) console.log('line is blank');

/^\s*$/ is a regex that means

 ^ anchor begin of string
 \s whitespace character class (space, tab, newline)
 *  zero or more times
 $ end of string
like image 90
PA. Avatar answered Apr 28 '23 13:04

PA.