0

I want to return false if "description" starts with a whitespace and true if it doesn't.

This is what I have at the moment. It works with IE10 but it doesn't work in IE8:

    var pattern = new RegExp(/^\s/);
    if (!pattern.test(description))
        return true;
    return false;

In IE8 it returns true all the time...

What am I doing wrong? :(

Matei
  • 327
  • 5
  • 17
  • Were there any errors in the console when you uses IE8? – chris97ong Jul 22 '14 at 11:02
  • 2
    Just a thought, but why not simply: `return (!pattern.test(description));` (this isn't an answer to your question, though, just a suggestion to reduce the clutter and simplify your code). – David Thomas Jul 22 '14 at 11:04
  • 2
    Works for me. You're creating the regular expression wrong, but it's harmless. It should be either `var pattern = /^\s/;` or `var pattern = new RegExp("^\\s");` But I just checked in IE8, and it works even if you do that, so that's not the actual problem. (Correctly defined: http://jsbin.com/ruhuv/1, incorrectly but harmless: http://jsbin.com/ruhuv/2) – T.J. Crowder Jul 22 '14 at 11:12
  • Probably the `RegExp` constructor started accepting another `RegExp` object between 8 and 10? – Ja͢ck Jul 22 '14 at 11:12
  • @Jack: Nope, works in IE8 too. In theory it should (just makes a round-trip through `String`), and I even remember that being a workaround for an old bug in Firefox around the `g` flag and literals... – T.J. Crowder Jul 22 '14 at 11:13
  • @T.J.Crowder Okay, then I agree the question is lacking a proper reproduce case. – Ja͢ck Jul 22 '14 at 11:13

2 Answers2

0

Here is how I fixed it.

    description = description.replace(/\u00a0/g, ' ');
    var pattern = new RegExp("^\\s");
    return (!pattern.test(description));

It seems that the spaces were actually generated from a  , which I replaced with normal spaces in the snippet above, then the regex worked!

Strange that I didn't have to do this for IE10, but IE8 had a problem.

This helped, I just didn't know which spaces I was dealing with: Replacing   from javascript dom text node

Community
  • 1
  • 1
Matei
  • 327
  • 5
  • 17
0

I had some problems with IE8 involving a search for substrings containing white spaces and their substitution by nothing ('') inside an element captured by outerHTML: that approach simply did not work; the IE8 Javascript engine returned the expected result + two white spaces in the beginning (I had to substitute all white spaces of the returned string by bullets, to visualize that) + inappropriate jQuery object information in the middle.

I modified my code, substituting in the original string all white space by bullets (•). Then I did a search for substrings containing bullets and substitute them by nothing (''), and the code worked well. As an example, follows a code to get the ID value of an element:

// DOES NOT WORK WITH IE8
var titleDiv = title.get(0).outerHTML;  // should return <div id="patrimonio">Patrimônio</div>
var idA = titleDiv.replace(/.+\sid=('|")?/, ''); // patrimonio">Patrimônio</div>
idA = idA.replace(/(\s|'|").+/g, ''); // patrimonio
// WORKS WITH IE8
var titleDiv = title.get(0).outerHTML.replace(/\s/g, '•');  //IE8 returns ••<div•id="patrimonio"•jQuery17209680172580078536="34">Patrimônio</div> , Others browsers return  <div•id="patrimonio">Patrimônio</div>
var idA = titleDiv.replace(/.+•id=('|")?/, '');
idA = idA.replace(/(•|'|").+/g, '');
aldemarcalazans
  • 1,309
  • 13
  • 16