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
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
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)
Related
var mystr = '\Data\Dashboard\myfolder\3.jpg';
mystr .replace(/\//g, '//');
It is removing all the slashes when i'm trying to replace. Can anyone tell me how to replace for this particular string.
End solution should be this '/Data/Dashboard/myfolder/3.jpg'
Any help will be appreciated!
Since you have \ in your string which is considered as escape sequence ( You need to escape it ). see console.log
var mystr = '\Data\Dashboard\myfolder\3.jpg';
console.log(mystr)
var mystr1 = '\\Data\\Dashboard\\myfolder\\3.jpg'; // Escaped '/' string
console.log(mystr1)
try with this one
var mystr = '\\Data\\Dashboard\\myfolder\\3.jpg';
var regex = /\\/g;
var replaced = mystr.replace(regex, '/');
console.log(replaced);
You can use the replace function:
str.replace('\\', "/");
I want to replace multiple occurences of comment and try like below
JsFiddle
Code:
var str = '<!--#test--><!--#test1-->'
str = str.replace('<!--/g', '').replace('-->/g', '');
alert(str)
Your problem is that you're trying to use a string instead of a regular expression. For example, this works.
var str = '<!--#test-->'
str = str.replace(/<!--/g, '').replace(/-->/g, '');
alert(str)
Plain regex commands need to be inside //.
Also, use the
Disjunction; Alternative | (pipe character)
str = str.replace(/<!--|-->/g, ''); // #test#test1
I have a url with many delimiters '/'.
I want to find the string after the last delimiter. How can I write a javascript code?
for eg if my url is
localhost/sample/message/invitation/create/email
I want to display 'email' as my output.
var last = input.split("/").pop();
Simples!
Splitting on a regex that matches spaces or hyphens and taking the last element
var lw = function(v) {
return (""+v).replace(/[\s-]+$/,'').split(/[\s-]/).pop();
};
lw('This is a test.'); // returns 'test.'
lw('localhost/sample/message/invitation/create/email,'); // returns 'email,'
var url="localhost/sample/message/invitation/create/email";
url.split("/").pop()
or
var last=$(url.split("/")).last();
Usng simple regex
var str = "localhost/sample/message/invitation/create/email";
var last = str.match(/[^/]*$/)[0]";
Above regex return all character after last "/"
I have tried replace space's with underscore's(_). Using bellow method's in java script
var stt="this is sample";
var stpp= stt.split(' ').join('_');
var stpp= stt.replace(' ','_');
but it will replace first space with underscore after that it will ignore all spaces.
results like
this_is sample
so how to replace all spaces with ( _ ) in sting using java script.
any one can help me.
Use String#replace with a regular expression using the global flag (g):
var stpp = stt.replace(/ /g, '_');
This:
str.replace(new RegExp(" ","g"),"_")
Or this:
var newstring = mystring.split(' ').join('_');
Need to replace a substring in URL (technically just a string) with javascript.
The string like
http://blah-blah.com/search?par_one=test&par_two=anothertest&SearchableText=TO_REPLACE
or
http://blah-blah.com/search?par_one=test&SearchableText=TO_REPLACE&par_two=anothertest
means, the word to replace can be either at the most end of the URL or in the middle of it.
I am trying to cover these with the following:
var newWord = NEW_SEARCH_TERM;
var str = 'http://blah-blah.com/search?par_one=test&SearchableText=TO_REPLACE&par_two=anothertest';
var regex = /^\S+SearchableText=(.*)&?\S*$/;
str = str.replace(regex, newWord);
But no matter what I do I get str = NEW_SEARCH_TERM. Moreover the regular expression when I try it in RegExhibit, selects the word to replace and everything that follows it that is not what I want.
How can I write a universal expression to cover both cases and make the correct string be saved in the variable?
str.replace(/SearchableText=[^&]*/, 'SearchableText=' + newWord)
The \S+ and \S* in your regex match all non-whitespace characters.
You probably want to remove them and the anchors.
http://jsfiddle.net/mplungjan/ZGbsY/
ClyFish did it while I was fiddling
var url1="http://blah-blah.com/search?par_one=test&par_two=anothertest&SearchableText=TO_REPLACE";
var url2 ="http://blah-blah.com/search?par_one=test&SearchableText=TO_REPLACE&par_two=anothertest"
var newWord = "foo";
function replaceSearch(str,newWord) {
var regex = /SearchableText=[^&]*/;
return str.replace(regex, "SearchableText="+newWord);
}
document.write(replaceSearch(url1,newWord))
document.write('<hr>');
document.write(replaceSearch(url2,newWord))