最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if a line only contain whitespace and n in jsnode.js - Stack Overflow

programmeradmin0浏览0评论

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
  • 2 The answer is don't write your own XML parser; use one from here: 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 Commented Sep 10, 2013 at 16:36
  • 1 \n is whitespace. see: MDN - \s – dc5 Commented Sep 10, 2013 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 Commented Sep 10, 2013 at 16:39
  • 1 @DrakaSAN, two of the answers there mention the non-jQuery xmljs.sourceforge.net . See also: stackoverflow.com/questions/8953040/… . 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 Commented Sep 10, 2013 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 Commented Sep 11, 2013 at 7:53
Add a comment  | 

3 Answers 3

Reset to default 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

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

发布评论

评论列表(0)

  1. 暂无评论