Reputation: 23
The usernames used by my organization are in the following format:
i:4#.w|abcd\1231231234.abc
I need to remove the \ and everything before it using JavaScript. I have the following code but because of the escape function of the \ in JS I find that JS simply remove \13 from the string. I have search for hours and haven't been able to find any solution. Here is the current code.
<script type="text/javascript">
window.onload = function () {
document.getElementById('userId').innerHTML ="i:4#.w|abmy\1391251254.abc";
}
</script>
<div>
<span id="userId"></span>
</div>
I need the result to be 1391251254.abc
Upvotes: 0
Views: 97
Reputation: 33726
An alternative is using the function substring
along with the function lastIndexOf
.
var str = "i:4#.w|abmy\\1391251254.abc"
str = str.substring(str.lastIndexOf('\\') + 1)
Upvotes: 0
Reputation: 2262
You can use a regex to extract the last part of your string. It will work event if the string contains more than one backslash.
var string = "i:4#.w|abmy\\1391251254.abc"; //Note the escaped '\'
var regex = /.*?\\(.*)$/;
var match = regex.exec(string);
console.log(match[1]); //1391251254.abc
Upvotes: 1