Replacing of string using javascript [duplicate] - javascript

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

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 create a function to split the following string? [duplicate]

This question already has answers here:
What is the shortest function for reading a cookie by name in JavaScript?
(20 answers)
Closed 2 years ago.
I have this string
"G_ENABLED_IDPS=app; COOKIE_EINF=someCookie; _ga=someGA;
_hjid=someHJID; _gcl_au=someglcau; COOKIE_EINF_SESS=somecookie1; _gid=somegid; _hjIncludedInPageviewSample=2; _hjTLDTest=3; _hjAbsoluteSessionInProgress=0; _hjIncludedInSessionSample=1; _gat_UA-124355-12=5"
And i need some sort of function to split this string given an argument , for example given that my string is text
text.split(";") , will split it into an array separating it by ";"
But i need a function like this
returnText(text , property) that would work like
returnText(text, "_gcl_au") --> returns "someglcau"
You could actually use a regex replacement approach here, for a one-liner option:
function returnText(text, property) {
var term = text.replace(new RegExp("^.*\\b" + property + "=([^;]+)\\b.*$", "gm"), "$1");
return term;
}
var input = "G_ENABLED_IDPS=app; COOKIE_EINF=someCookie;_ga=someGA;_hjid=someHJID; _gcl_au=someglcau; COOKIE_EINF_SESS=somecookie1; _gid=somegid; _hjIncludedInPageviewSample=2; _hjTLDTest=3; _hjAbsoluteSessionInProgress=0; _hjIncludedInSessionSample=1; _gat_UA-124355-12=5";
console.log(returnText(input, "_gcl_au"));
you can use split, just as you tried:
function returnText(text , property){
entries = text.split('; ');
const newEntries = [];
entries.forEach(item => {
let vals = item.split('=');
newEntries[vals[0]] = vals[1]
});
return newEntries[property];
}
const text = "G_ENABLED_IDPS=app; COOKIE_EINF=someCookie; _ga=someGA;_hjid=someHJID; _gcl_au=someglcau; COOKIE_EINF_SESS=somecookie1; _gid=somegid; _hjIncludedInPageviewSample=2; _hjTLDTest=3; _hjAbsoluteSessionInProgress=0; _hjIncludedInSessionSample=1; _gat_UA-124355-12=5";
console.log(returnText(text,'_gcl_au'));

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

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

Categories

Resources