How can I replace '/' in a Javascript string with another character [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript replace all / with \ in a string?
I have a date='13/12/2010' I want to replace this '/' to '_' or something else.
I read this But I do not know how can that applied for my case .

Use a global RegEx (g = global = replace all occurrences) for replace.
date = date.replace(/\//g, '_');
\/ is the escaped form of /. This is required, because otherwise the // will be interpreted as a comment. Have a look at the syntax highlighting:
date = date.replace(///g, '_');

One easiest thing :)
var date='13/12/2010';
alert(date.split("/").join("_")); // alerts 13_12_2010
This method doesn't invoke regular expression engine and most efficient one

You can try escaping the / character like this -
date.replace( /\//g,"_");

Related

String starting with backslash hacks my regex [duplicate]

This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
Closed 3 years ago.
My string should be in the IRC command format : "/add john".
So, i created this Regex :
var regex = /^\/add ([A-Za-z0-9]+)$/
var bool = regex.test('\/add user1');
alert(bool);
The problem is either I use /***/ or RegExp syntax, if I set a backslash at the beginning of my string (like in my example above), my alert pop up show "true" and I don't want that.
I code in Javascript
You can use String.raw to make sure that the backlash is not removed when testing your input:
var regex = /^\/add ([A-Za-z0-9]+)$/
var bool = regex.test(String.raw`\/add user1`);
alert(bool);
You can play with this code here: https://jsbin.com/ziqecux/25/edit?js

How do I insert something at a specific character with Regex in Javascript [duplicate]

This question already has answers here:
Simple javascript find and replace
(6 answers)
Closed 5 years ago.
I have string "foo?bar" and I want to insert "baz" at the ?. This ? may not always be at the 3 index, so I always want to insert something string at this ? char to get "foo?bazbar"
The String.protype.replace method is perfect for this.
Example
let result = "foo?bar".replace(/\?/, '?baz');
alert(result);
I have used a RegEx in this example as requested, although you could do it without RegEx too.
Additional notes.
If you expect the string "foo?bar?boo" to result in "foo?bazbar?boo" the above code works as-is
If you expect the string "foo?bar?boo" to result in "foo?bazbar?bazboo" you can change the call to .replace(/\?/g, '?baz')
You don't need a regular expression, since you're not matching a pattern, just ordinary string replacement.
string = 'foo?bar';
newString = string.replace('?', '?baz');
console.log(newString);

Parsing regex in javascript [duplicate]

This question already has answers here:
JavaScript Regex, where to use escape characters?
(3 answers)
Closed 8 years ago.
I'm unable to parse a regex. I've tested it from regexpal.com and regex101.com (by setting the expression as "(?:^csrf-token|;\s*csrf-token)=(.*?)(?:;|$)" and the test string as "__ngDebug=false; csrf-token=b2ssOJ4jNOlPdmXAHn4CORPvFoO4Ngsupej25tdj") where it works.
The example is jsfiddle.
function getCookie(name) {
var s = "__ngDebug=false; csrf-token=b2ssOJ4jNOlPdmXAHn4CORPvFoO4Ngsupej25tdj";
var regexp = new RegExp("(?:^" + name + "|;\s*"+ name + ")=(.*?)(?:;|$)", "g");
var result = regexp.exec(s);
return (result === null) ? null : result[1];
}
alert(getCookie("csrf-token"));
If however s is "csrf-token=b2ssOJ4jNOlPdmXAHn4CORPvFoO4Ngsupej25tdj", then it works fine. Please tell me what's wrong.
The expected output is "b2ssOJ4jNOlPdmXAHn4CORPvFoO4Ngsupej25tdj", and there is no input (the string to be tested is 's').
Change
"|;\s*"
to
"|;\\s*"
^
The thing is, you are constructing the RegExp by passing in a string via the constructor, so you need to follow the rule of escaping in string literal. In JavaScript, "\s" is recognized as a single character string, with lowercase s. To specify \, you need to escape it.
You should escape \s
var regexp = new RegExp("(?:^" + "csrf-token" + "|;\\s*"+ "csrf-token" + ")=(.*?)(?:;|$)", "g");
^

How can I replace Space with %20 in javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
jQuery / Javascript replace <space> in anchor link with %20
I am getting sParameter like this :
sParameter = document.getElementById('ddParameterType').value;
If I am getting word like "Test - Text" as the ddParameterType item, then I am replacing the space in word like below:
sParameter = document.getElementById('ddParameterType').value.replace("","%20");
but it is returning a valur like %20Test - Text. I need like Test%20-%20Text.
sParameter = encodeURIComponent(sParameter.trim()) //"Test%20-%20Text"
the .trim will remove leading and trailing whitespace from the string. encodeURIComponent will URL-encode it.
replace replace("","%20"); with replace(/ /g,"%20");
http://www.w3schools.com/jsref/jsref_replace.asp
sParameter = encodeURIComponent(sParameter.trim())
Use the following instead to replace all occurrences:
document.getElementById('ddParameterType').value.replace(/ /g, "%20");
Or better yet:
encodeURIComponent(document.getElementById('ddParameterType').value);

javascript - How to do replaceAll? [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 6 years ago.
Hi I have a problem here. I am trying to replace all instances of + character in a string using javascript. What happens is that only the first instance is being changed.
Here is my code:
var keyword = "Hello+Word%+";
keyword = keyword.replace("+", encodeURIComponent("+"));
alert(keyword);
The output is Hello%2BWord%+ when it should be Hello%2BWord%%2B because there are 2 instances of +.
You can check this on : http://jsfiddle.net/Wy48Z/
Please help. Thanks in advance.
You need the global flag.
Fixed for you at http://jsfiddle.net/rtoal/Wy48Z/1/
var keyword = "Hello+Word%+";
keyword = keyword.replace(/\+/g, encodeURIComponent("+"));
alert(keyword);​
The javascript regex, which is done by putting the expresison inbetween two forward slashes like: /<expression/
If you want to replace all, simply append a g after the last one like:
/<expression/g
In your case, it would be /\+/g
The cross-browser approach is to use a regexp with the g (global) flag, which means "process all matches of the pattern, not just the first":
keyword = keyword.replace(/\+/g, encodeURIComponent("+"));
Notice I prefix the plus sign with a backslash because it would otherwise have the special meaning of "match one or more of the preceding thing".

Categories

Resources