how to remove forward slash from my string? - javascript

i have string like
var str='Dowagiac\'s Olympic wrestler recalled'
i want to remove (forward slash) from string.
var str1=str.replace(/\'/g, '\\\'');
alert(str1);

No regex, but it will still do the trick.
str = str.split('\\').join('');

just replace "\" with "".
var str="Dowagiac\'s Olympic wrestler recalled";
foo = str.replace("\\","");
alert(foo);
fiddle

Related

Can't able to replace forward slash with backward slash

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('\\', "/");

Removing spaces, hyphen and brackets from a string

I am getting a string like:
var str = '+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)';
I want to remove the spaces, hyphen and brackets from every number. Something like:
var str = '+911234567891,432123234,12312313456,4325671234';
Please suggest a way to achieve this.
This will do your job:
var str = '+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)';
var result = str.replace(/[- )(]/g,'');
alert(result);
You can use Regular Expression to replace those items by empty string:
'+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)'.replace(/[\s()-]+/gi, '');
// results in "+911234567891,432123234,12312313456,4325671234"
Hope it helps.

String replace last character occurrence slash

My url looks like this: ://example/example/.com. I want to remove the last slash of the string. My attempt so far (but doesn't work):
.replace(/\/$/g, '');
Can someone help me along?
You have to escape the slash character in a regular expression literal. Capture the characters after the last slash until the end of the string and use in the replacement:
s = s.replace(/\/([^\/]*)$/, '$1');
(You don't need the g flag for this one, as you know that there is never more than one match.)
Demo: http://jsfiddle.net/Guffa/jkn52/
Alternatively, use a poositive look-ahead to match a slash that doesn't have another slash until the end of the string:
s = s.replace(/\/(?=[^\/]*$)/, '');
Demo: http://jsfiddle.net/Guffa/jkn52/2/
var str = "/1/2/3/4/5"
var index = str.lastIndexOf("/");
var newStr = str.substr(0, index ) + str.substr(index + 1);
console.log(newStr);
demo: http://jsfiddle.net/Jn9bm/
It's a little verbose, but it works:
var url = "//example/example/.com";
var slash_position = url.lastIndexOf('/');
url = url.substr(0, slash_position) + url.substr(slash_position+1);
Try This:
var s= someString.replace(/\//g, "");

Javascript replace regex wildcard

I have a string which I need to run a replace.
string = replace('/blogs/1/2/all-blogs/','');
The values 1, 2 and all-blogs can change. Is it possible to make them wildcards?
Thanks in advance,
Regards
You can use .* as a placeholder for "zero or more of any character here" or .+ for "one or more of any character here". I'm not 100% sure exactly what you're trying to do, but for instance:
var str = "/blogs/1/2/all-blogs/";
str = str.replace(/\/blogs\/.+\/.+\/.+\//, '');
alert(str); // Alerts "", the string is now blank
But if there's more after or before it:
str = "foo/blogs/1/2/all-blogs/bar";
str = str.replace(/\/blogs\/.+\/.+\/.+\//, '');
alert(str); // Alerts "foobar"
Live example
Note that in both of the above, only the first match will be replaced. If you wanted to replace all matches, add a g like this:
str = str.replace(/\/blogs\/.+\/.+\/.+\//g, '');
// ^-- here
You can read up on JavaScript's regular expressions on MDC.
js> 'www.google.de/blogs/1/2/all-blogs'.replace(/\/blogs\/[^\/]+\/[^\/]+\/[^\/]+\/?/, '');
www.google.de
What about just splitting the string at slashes and just replacing the values?
var myURL = '/blogs/1/2/all-blogs/', fragments, newURL;
fragments = myURL.split('/');
fragments[1] = 3;
fragments[2] = 8;
fragments[3] = 'some-specific-blog';
newURL = fragments.join('/');
That should return:
'/blogs/3/8/some-specific-blog'
Try this
(/.+){4}
escape as appropriate

Javascript regex

Currently I have a basic regex in javascript for replacing all whitespace in a string with a semi colon. Some of the characters within the string contain quotes. Ideally I would like to replace white space with a semi colon with the exception of whitespace within quotes.
var stringin = "\"james johnson\" joe \"wendy johnson\" tony";
var stringout = stringin.replace(/\s+/g, ":");
alert(stringout);
Thanks
Robin
Try something like this:
var stringin = "\"james johnson\" joe \"wendy johnson\" tony";
var stringout = stringin.replace(/\s+(?=([^"]*"[^"]*")*[^"]*$)/g, ":");
Note that it will break when there are escaped quotes in your string:
"ab \" cd" ef "gh ij"
in javascript, you can easily make fancy replacements with callbacks
var str = '"james \\"bigboy\\" johnson" joe "wendy johnson" tony';
alert(
str.replace(/("(\\.|[^"])*")|\s+/g, function($0, $1) { return $1 || ":" })
);
In this case regexes alone are not the simplest way to do it:
<html><body><script>
var stringin = "\"james \\\"johnson\" joe \"wendy johnson\" tony";
var splitstring = stringin.match (/"(?:\\"|[^"])+"|\S+/g);
var stringout = splitstring.join (":");
alert(stringout);
</script></body></html>
Here the complicated regex containing \\" is for the case that you want escaped quotes like \" within the quoted strings to also work. If you don't need that, the fourth line can be simplified to
var splitstring = stringin.match (/"[^"]+"|\S+/g);

Categories