Regex replace with captured [duplicate] - javascript

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);

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.

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 can I do string replace in jquery [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 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);
});

Return only a part of the match [duplicate]

This question already has an answer here:
Get part of the string using regexp [closed]
(1 answer)
Closed 9 years ago.
Here's an example http://jsbin.com/USONirAn/1
var string = "some text username#Jake# some text username#John# some text some text username#Johny# userphoto#1.jpg#";
var type = "username";
var regexp = new RegExp(type + "#(.*?)#");
var matches = string.match(regexp);
Current regexp returns into matches an array with 3 items - [username#Jake#, username#John#, username#Johny#].
How do I make it return only a strings that I used to search for - (.*?)? In this example is should be an array [Jake, John, Johny]. Is it possible to get this only by changing a regexp function?
Update:
I've also tried to use exec function, but it returns both [username#Jake#, Jake] http://jsbin.com/USONirAn/6
Search-and-don't-replace
var matches = []
string.replace(regexp, function () {
matches.push(arguments[1]);
});
http://jsbin.com/USONirAn/4

Replacing of string using javascript [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 9 years ago.
I want to replace string from "_tr9-9" character from a string and replace them with _id character
Here str is changes only _tr9-0 will be changes dynamically remining will be same
str=Test_User_tr9-0;
Ex:
function (str)
{
var obj=str
}
In C#
string str = "Test_user_tr9-9";
string str2 = str.Replace("_tr9-9", "_id");
In Javascript
var str = "Test_user_tr9-9";
var str2 = str.replace("_tr9-9", "_id");
Note that both in Javascript and C# strings are immutable objects, so the replace/Replace methods return a new modified string (technically in C# the Replace returns the original string if it doesn't find anything to replace)
In JavaScript:
var index = str.lastIndexOf("_");
var result = str.substring(0, index) + "_id";
jsFiddle: http://jsfiddle.net/fNZkG/
You can use replace
In javascript
var str = "Test_user_tr9-9 to Test_user_id";
str = str.replace('tr9-9','id');
In C#
var str = "Test_user_tr9-9";
var str = str.Replace("_tr9-9", "_id");

Categories

Resources