regex - Stripping javascript unicode character 8206 from a string - Stack Overflow

I'm still new to JavaScript (VB.NET previously) and ing to grips with the language. I have a probl

I'm still new to JavaScript (VB.NET previously) and ing 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... :-)

I'm still new to JavaScript (VB.NET previously) and ing 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... :-)

Share Improve this question asked Sep 22, 2013 at 9:35 deandobdeandob 5,2805 gold badges28 silver badges40 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744839424a4596463.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信