开发者

Simple Replace \ with *

开发者 https://www.devze.com 2022-12-19 14:08 出处:网络
I have a paragraph that currently has C:\\ How can I use jQuery to change it开发者_JAVA技巧 to be C:*Careful! JavaScript\'s string.replace doesn\'t behave the same as other languages. It will only re

I have a paragraph that currently has C:\

How can I use jQuery to change it开发者_JAVA技巧 to be C:*


Careful! JavaScript's string.replace doesn't behave the same as other languages. It will only replace one occurrence of the matched string. If you want to do a global search-and-replace you have to use a RegExp object so you can set the g flag:

el.text(el.text().replace(/C:\\/g, 'C:*'));

An alternative to regexcersizing the replace operation (especially useful when you have an arbitrary string to replace that might contain regex-special characters), is the JS split-and-join replacement idiom:

el.text(el.text().split('C:\\').join('C:*'));


Pretty simple:

var p = $("#theparagraph");
p.text(p.text().replace("C:\\", "C:*"));


var para = $("p"); //Get your paragraph
para.text(para.text().replace(/C:\/gi, "C:*");


You can use the javascript replace function:

var s= 'C:\\';
s= s.replace('C:\\', 'C:*');
alert(s);


With jQuery 1.4, you can pass a function into any setter function to manipulate it's value.

jQuery('p').text(function(i, text) {
    return text.replace(/C:\\/g, 'C:*');
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号