Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripping javascript unicode character 8206 from a string

I'm still new to JavaScript (VB.NET previously) and coming to grips with the language. I have a problem where a string I'm passing around in JavaScript has somehow got Unicode escape characters in it (0x5206, left-to-right mark) that I need to strip out. I can do this with a dumb loop checking for the existence of the unicode character and stripping it out, but I should be able to do this with a .replace(regex). However after a couple of hours trying I need help with it (first time doing regex).

// Convert from JS date format to dotnet ticks
function getNetDateTime(JSDate, dateortime) {
    var convStr = JSDate.toLocaleString();
    var tryRegEx = convStr.replace(/\u5206/g, "");
    var tempStr = "1/01/2000 10:00:00 AM";
    var stripStr = "";
    for (var i = 0; i < convStr.length; i++) {
        if (convStr.charCodeAt(i).toString() != "8206") stripStr = stripStr + convStr.charAt(i);
    }
    alert(new Date(tempStr).toString());
    alert(new Date(tryRegEx).toString());
    alert(new Date(stripStr).toString())
    var datetime = ((new Date(stripStr).valueOf() + unixEpoc) * pcTicks).toString();
    return datetime;
}

In the code above, tempStr and stripStr give the correct date but not tryRegEx (gets invalid date as the Unicode escape char isn't stripped out). I'm fairly sure the regex expression is correct so not really sure why regex isn't working for me (so I can ditch the loop).

Thanks for any pointers. I'm sure I'm overlooking something basic here... :-)

like image 298
deandob Avatar asked Sep 22 '13 09:09

deandob


1 Answers

5206 or 8206? Pick one :) If it's 8206, you want \u200E (8206 in hex). If it's 5206 you want \u1456

like image 79
Nobody Avatar answered Oct 12 '22 13:10

Nobody