Javascript search and replace not working
I have a string like this,
"<?xml version="1.0" encoding="utf-8"?><feed xml:base="https://9999.example.com.us/_vti_bin/MYServices/MYServices.svc/ ... so on .....
I want it to be this,
"<?xml version="1.0" encoding="utf-8"?><feed xml:base="https://9999.example.com.us/myPart/myPart123/_vti_bin/MYServices/MYServices.svc/ ... so on
These are large xml Odata responses and I am trying to replace base url as above, I tried this but it isn't working,
String.prototype.replaceAll = function (find, replace) { var str = this; return str.replace(new RegExp(find.replace(/[-/^$*+?.()|[]{}]/g, '$&'), 'g'), replace); };
string = '<?xml version="1.0" encoding="utf-8"?><feed xml:base=https://9999.example.com.us/_vti_bin/MYServices/MYServices.svc';
string = string.replaceAll('<?xml version="1.0" encoding="utf-8"?><feed xml:base=https://9999.example.com.us/_vti_bin/MYServices/MYServices.svc',
'<?xml version="1.0" encoding="utf-8"?><feed xml:base=https://9999.example.com.us/myPart/myPart123/_vti_bin/MYServices/MYServices.svc');
$('#result').html(string);
Update
I just want to add this part to the url, "/myPart/myPart123/"
http://jsfiddle.net/cdbzL/509/
Maybe this sample will help you:
var xml = '<?xml version="1.0" encoding="utf-8"?><feed xml:base="https://9999.example.com.us/_vti_bin/MYServices/MYServices.svc/';
xml.replace(/_vti_bin/img, 'myPart/myPart123/_vti_bin');
You can use only replace()
function to do that. Read more about It.
And replace $('#result').html(string)
with $('#result').text(string)
.
Some words about your String.prototype.replaceAll = function() {}
:
Object-Oriented JavaScript - Second Edition : Augmenting built-in objects through the prototype is a powerful technique, and you can use it to shape JavaScript in any way you like. Because of its power, though, you should always thoroughly consider your options before using this approach. The reason is that once you know JavaScript, you're expecting it to work the same way, no matter which third-party library or widget you're using. Modifying core objects could confuse the users and maintainers of your code and create unexpected errors.
Demo
链接地址: http://www.djcxy.com/p/94216.html上一篇: SQL根据ID匹配从一个表更新到另一个表
下一篇: Javascript搜索和替换不工作