4

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?

DrakaSAN
  • 7,673
  • 7
  • 52
  • 94
  • 2
    The answer is don't write your own XML parser; use one from here: http://stackoverflow.com/questions/83405/xml-parser-for-javascript (but if you really do just want to learn to write an XML parser, look at regular expressions for parsing text). – 15ee8f99-57ff-4f92-890c-b56153 Sep 10 '13 at 16:36
  • 1
    `\n` is whitespace. see: [MDN - \s](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-white-space) – dc5 Sep 10 '13 at 16:39
  • They are for JQuery, I use node.js, and did not found any xml parser for node.js. I ll add that smil isn t exactly xml, but have a similar syntax. – DrakaSAN Sep 10 '13 at 16:39
  • 1
    @DrakaSAN, two of the answers there mention the non-jQuery http://xmljs.sourceforge.net/ . See also: http://stackoverflow.com/questions/8953040/node-js-xml-parser-on-windows . Writing your own XML parser will be a great learning experience, though, if you've got the tenacity to see it through at your level of experience. – 15ee8f99-57ff-4f92-890c-b56153 Sep 10 '13 at 17:35
  • @EdPlunkett: Well I finished my parser, it s not perfect and is quite strict on how the file must be writed (order of the fields), but it work and avoid me to rewrite other part of my code which were dependent of the return type. If you want, I can post it in a edit. I ll give a look at your solution thought. – DrakaSAN Sep 11 '13 at 07:53

3 Answers3

15

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
PA.
  • 28,486
  • 9
  • 71
  • 95
2

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, '');
  };
}
jsist
  • 5,223
  • 3
  • 28
  • 43
  • Trim does work (since it s simple javascript), and you had a good point about line.trim().length, it is 0. – DrakaSAN Sep 10 '13 at 16:46
0

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

WickedFan
  • 356
  • 2
  • 12