Javascript String replace for '&' not working [duplicate] - javascript

This question already has answers here:
Regex replace('/color[1-9]?[0-9]/g','') not working in JavaScript
(5 answers)
Closed 8 years ago.
Code as below
var data = '<address>Test String & Test</address>';
var cleanData = data.replace('/&/g', "and");
I would like to replace all occurrences of '&'
JSFiddle http://jsfiddle.net/d33wyw6g/
Thanks in advance.

When you use a regex, do not place it between quotes, you can use it directly:
var data = '<address>Test String & Test</address>';
var cleanData = data.replace(/&/g, "and");
This will replace all "&" and not only the first one
Demo here: http://jsfiddle.net/d33wyw6g/2/

try var cleanData = data.replace(/&/g, "and");
Demo

Try this.
data.replace("&", "and");

Related

how to detect and get url on string javascript [duplicate]

This question already has answers here:
Javascript: extract URLs from string (inc. querystring) and return array
(5 answers)
Closed 7 years ago.
how to detect and get url on string javascript?
example :
var string = "hei dude, check this link http:://google.com and http:://youtube.com"
how to get result like this from my string :
var result = ["http:://google.com", "http:://youtube.com"]
how do that?
You input has double :: after http. Not sure if it intentional. If it is then use:
var matches = string.match(/\bhttps?::\/\/\S+/gi);
If only one : is needed then use:
var matches = string.match(/\bhttps?:\/\/\S+/gi);
RegEx Demo
const string = "hei dude, check this link http:://google.com and http:://youtube.com"
const matches = string.match(/\bhttp?::\/\/\S+/gi);
console.log(matches);

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

Replace string having , with $ [duplicate]

This question already has answers here:
JavaScript - Replace all commas in a string [duplicate]
(3 answers)
Closed 8 years ago.
Below is my string.When I console b it is showing as below output:
var a='602,315,805,887,810,863,657,665,865,102,624,659,636';
var b = a.replace(',',"$");
console.log(b);
output:
602$315,805,887,810,863,657,665,865,102,624,659,636
What should I do to replace complete commas in string to $.
Use regexp, /,/g with global flag
var a ='602,315,805,887,810,863,657,665,865,102,624,659,636';
var b = a.replace(/,/g,"$");
Example
This question already has an answer anyway i have provide a different approach
var var a ='602,315,805,887,810,863,657,665,865,102,624,659,636';
var change= '$'
a= a.split(',').join(change);
You can use String methods .split() and .join() to make an array then glue the pieces back together.
var b = a.split(',').join('$');
str.replace(/,/g,"$");
will replace , with $
DEMO

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

how to replace all %s string in javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript equivalent to printf/string.format
replace all occurrences in a string
see title. see the following variable, how can i replace all '%s' with other string?
var s = 'qwer%sqwer%s';
please advice
Use .replace method.
var s = 'qwer%sqwer%s';
s = s.replace(/%s/g, 'other');
Try this:
s.replace(/%s/g,'x')
You can Use replace() function for replacing string
Ex.
var getnewstring = s.replace("%s","YOUR STRING");
Use the .replace method
var s = 'qwer%sqwer%s';
s=s.replace('%s','your string')
document.write(s);

Categories