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?
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?
Share Improve this question asked Sep 10, 2013 at 16:33 DrakaSANDrakaSAN 7,8538 gold badges56 silver badges96 bronze badges 5 |3 Answers
Reset to default 15read 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
Trim removes all the leading and trailing WHITESPACES
. I think it will also remove \n and \t as well. You should check for the length of the string to be zero after it is trimmed.
If length is not zero then string must be containing characters other than whitespaces.
As you mentioned about Node.js. If trim is not working then you can implement trim by simply executing following code:
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
}
I would try a different approach; find the types of characters you're after.
var lineR = line.search(char(34)) //char(34) is carriage return
if(lineR != null)
//lineR is your actual position of lineReturn. Do a Substring(0,lineR) then.
line = line.Substring(0,lineR)
Alternatively, look into .lastIndexOf(). http://www.w3schools.com/jsref/jsref_lastindexof.asp
\n
is whitespace. see: MDN - \s – dc5 Commented Sep 10, 2013 at 16:39