How can I do string replace in jquery [duplicate] - javascript

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 6 years ago.
I have this code
$("#title").keyup(function(){
var titleval = $("#title").val();
var res = titleval.replace(" ", "-");
$("#newsurl").val(res);
});
to replace spaces into dash to get URL like this
wordone-wordtow-wordthree
but i have problem with this code it's just replace first space like this
wordone-wordtow wordthree
How can i solve this problem

You need to do a global match, you can do this with a regex
var res = titleval.replace(/\s/g, "-");
Though String.prototype.replace does support having flags passed, this is deprecated in firefox and already doesn't work in chrome/v8.

Alternate method (if regex is not mandatory) could be to split and join
var res = titleval.split(" ").join("-");
or
var res = titleval.split(/\s+/).join("-");

Use regex with global flag
titleval.replace(/\s/g, "-");

try like this:
$("#title").keyup(function(){
var titleval = $("#title").val();
var res = titleval.replace(/\s+/g, '-');
$("#newsurl").val(res);
});

Related

Cannot replace all ^ symbol to 25%5E [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 8 months ago.
I have replace the problem in react. I want ^ to 25%5E, but I just can replace one of ^ in the string, cannot replace all ^ symbol to 25%5E. Below is my sample code:
var urlStr = "http://localhost:3005/branch-management/edit-branch/?companyName=ABC%20SDN%20BHD%20!!!!%40%40%40%40%23%24%^%26*()&branchName=ABC%20!%40%23%24%^%26*()_";
var newUrlStr = urlStr.replace("^", "25%5E");
console.log(newUrlStr);
Error Result:
How to solve this problem?
You should use replaceAll which is for replacing all matched strings
var urlStr = "http://localhost:3005/branch-management/edit-branch/?companyName=ABC%20SDN%20BHD%20!!!!%40%40%40%40%23%24%^%26*()&branchName=ABC%20!%40%23%24%^%26*()_";
var newUrlStr = urlStr.replaceAll("^", "25%5E");
console.log(newUrlStr);
You also can use regex with /g (a global flag) to have similar behaviour
var urlStr = "http://localhost:3005/branch-management/edit-branch/?companyName=ABC%20SDN%20BHD%20!!!!%40%40%40%40%23%24%^%26*()&branchName=ABC%20!%40%23%24%^%26*()_";
var newUrlStr = urlStr.replace(/\^/g, "25%5E");
console.log(newUrlStr);
use regex to find all occurence of ^ and replace.
var urlStr = "http://localhost:3005/branch-management/edit-branch/?companyName=ABC%20SDN%20BHD%20!!!!%40%40%40%40%23%24%^%26*()&branchName=ABC%20!%40%23%24%^%26*()_";
var newUrlStr = urlStr.replace(/\^/g, "25%5E");
console.log(newUrlStr);
Use the replaceAll() method
var urlStr = "http://localhost:3005/branch-management/edit-branch/?companyName=ABC%20SDN%20BHD%20!!!!%40%40%40%40%23%24%^%26*()&branchName=ABC%20!%40%23%24%^%26*()_";
var newUrlStr = urlStr.replaceAll("^", "25%5E");
console.log(newUrlStr);
the replace method either takes a pair of char's or a pair of CharSequence's (of which String is a subclass, so it'll happily take a pair of String's).
The replace method will replace all occurrences of a char or CharSequence. On the other hand, the first String arguments of replaceFirst and replaceAll are regular expressions (regex). Using the wrong function can lead to subtle bugs.

Regex replace with captured [duplicate]

This question already has answers here:
Using $0 to refer to entire match in Javascript's String.replace
(2 answers)
Closed 5 years ago.
I want replace all non alphanumeric character in the string by it self surrender for "[" and "]".
I tried this:
var text = "ab!#1b*. ef";
var regex = /\W/g;
var result = text.replace(regex, "[$0]");
console.log(result);
I was expecting to get:
ab[!][#]1b[*][.][ ]ef
But instead i get:
ab[$0][$0]1b[$0][$0][$0]ef
How can I do this using Javascript(node)?
You need to wrap the group in parentheses to assign it to $1, like this:
var text = "ab!#1b*. ef";
var regex = /(\W)/g;
var result = text.replace(regex, "[$1]");
console.log(result);

Javascript regex matching syntax not working [duplicate]

This question already has an answer here:
Javascript regex match fails on actual page, but regex tests work just fine
(1 answer)
Closed 5 years ago.
I would like to test a string to ensure it has the following pattern:
asas/asas2/asas
The charatcers in between the slashes can be letters, digits or both.
I have a working example here, although i'm sure it could be improved.
Regex Example
But when tested in jsfiddle it doesn't work
Jsfiddle Example
var str = 'dfdfdf/dfdf/dfdf';
var patt = new RegExp("/(^\w+\/\w+\/\w+$)/g");
var res = patt.test(str);
alert(res);
The above code example always returns false.
Remove the quotes new RegExp(/(^\w+\/\w+\/\w+$)/g);
You just have to remove quotes.
var str = 'dfdfdf/dfdf/dfdf';
var patt = /(^\w+\/\w+\/\w+$)/g;
var res = patt.test(str);
alert(res);
Please try this code:
var str = 'dfdfdf/dfdf/dfdf';
var patt = new RegExp(/(^\w+\/\w+\/\w+$)/g);
var res = patt.test(str);
alert(res);
console.log(res);
Thanks

How to delete all words with "-" before in Javascript [duplicate]

This question already has answers here:
Regex, replace all words starting with #
(5 answers)
Closed 5 years ago.
I'm totally new to regex and can't figure out how to realize it via Javascript.
For example, I have the string var string = "-just an example -string for stackoverflow". The expected result is string = "an example for stackoverflow".
Thanks in advance
Try this:
-\w+\s+
and replace by empty
Regex Demo
const regex = /-\w+\s+/gm;
const str = `-just an example -string for stackoverflow`;
const subst = ``;
const result = str.replace(regex, subst);
console.log(result);
Try this with simple forEach.
var string = "-just an example -string for stackoverflow";
var strArray = string.split(' ');
strArray.forEach(function(value, i){
if(value.startsWith('-')){
strArray.splice( i, 1 )
}
});
console.log(strArray.join(' '));

How to use replaceAll() in Javascript.........................? [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 6 years ago.
I am using below code to replace , with \n\t
ss.replace(',','\n\t')
and i want to replace all the coma in string with \n so add this ss.replaceAll(',','\n\t') it din't work..........!
any idea how to get over........?
thank you.
You need to do a global replace. Unfortunately, you can't do this cross-browser with a string argument: you need a regex instead:
ss.replace(/,/g, '\n\t');
The g modifer makes the search global.
You need to use regexp here. Please try following
ss.replace(/,/g,”\n\t”)
g means replace it globally.
Here's another implementation of replaceAll.
String.prototype.replaceAll = function (stringToFind, stringToReplace) {
if (stringToFind === stringToReplace) return this;
var temp = this;
var index = temp.indexOf(stringToFind);
while (index != -1) {
temp = temp.replace(stringToFind, stringToReplace);
index = temp.indexOf(stringToFind);
}
return temp;
};
Then you can use it like:
var myText = "My Name is George";
var newText = myText.replaceAll("George", "Michael");

Categories

Resources