Could not replace hypen from string using javascript [duplicate] - javascript

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 3 years ago.
I need to replace hypen from string using Javascript but as per my code its not working.
function replace(){
var str='20-03-2019';
str.replace(/-/g,'/');
console.log(str);
}
replace();
Here I need to replace hypen(-) with / and output should like 20/03/2019.

String is immutable, so you need to do :
function replace(){
var str='20-03-2019';
str = str.replace(/-/g,'/');
alert('date ' + str);
}
replace();

Related

Is there is a way to replace multiple character with replace function? [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 1 year ago.
my replace function only replace one character.
My Code example
const string = "What,Yo,Yes"
string.replace(","," ")
Console
"What Yo,Yes"
Something like this:
const string = "What,Yo,Yes";
console.log(string.replace(/,/g," "));
Alternatively, you can use replaceAll, like this:
const string = "What,Yo,Yes";
console.log(string.replaceAll(","," "));

How to cut one string to sententce and capitalize each letter of words [duplicate]

This question already has answers here:
Convert camelCaseText to Title Case Text
(26 answers)
Closed 2 years ago.
I am new in Javascript. I have a little problem:
I have a string like this "thisIsATestString".
So how can I make this string to "This Is A Test String"?
Already answered: Convert camelCaseText to Sentence Case Text
You should have searched for "camel case to sentence."
This should help to resolve it.
const sentence = 'thisIsATestString';
const updatedSentenec = (sentence[0].toUpperCase() + sentence.slice(1)).split(/(?=[A-Z])/).join(' ')
Try this:
"thisIsATestString".replace(/(^\w|[A-Z])/g, function (match) {
return ' ' + match.toUpperCase();
}).trim();

javascript replace instance of string [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 5 years ago.
I want to replace all instance of particular string using javascript/jquery.
My source string is '\\n' and my desired output is '\n'.
this should happen at all instance with in a string.
var str = "hii All, \\n I am sambo. \\n Thank you for listening.";
var res = str.replace(/\\n/gi, "\n");
console.log(str);
Use replace with regex.

Remove Substring in Javascript [duplicate]

This question already has answers here:
How to remove text from a string?
(16 answers)
Closed 7 years ago.
In JavaScript, I need to find a substring between &J= and Key using regex and remove it from my url which could contain several substrings.
Here is my url:
SID=18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003
Thanks
You can learn Regular Expression here and code for your problem is:
Here you go:
SID="18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003";
var re = /&J=(.*?)&Key/;
SID = SID.replace(re, '');
alert(SID);
See Fiddle

how to get the value(ID) using javascript substring? [duplicate]

This question already has answers here:
Get string inside parentheses, removing parentheses, with regex
(3 answers)
Closed 9 years ago.
I am trying to extract a string from within a larger string where i need to get the value only inside the brackets.
var str = 'ajay kumar (68766)';
Try this:
var str = 'ajay kumar (68766)';
str = str.slice(str.indexOf('(')+1, str.indexOf(')'));
How about using a regular expression?
var str = 'ajay kumar (68766)';
var output = str.replace(/^[\s\S]*?\((\d+)\)[\s\S]*?$/, '$1');

Categories