admin管理员组文章数量:1332359
I am trying to replace a string starting with a specific symbol '@' with the symbol '%', but the condition is that the symbol should be at the start of the string.
For eg.
@@@hello@hi@@
should be replaced by
%%%hello@hi@@
I have e up with the regex that matches the starting '@' symbols, but I am able to replace it only once, instead of replacing it with the number of times it matched.
The code is
var str = "@@@hello@hi@@";
var exp = new RegExp('^@+', 'g');
var mystr = str.replace(exp, '%');
But, it outputs
%hello@hi@@
But, the intended output is
%%%hello@hi@@
My current solution is something like this:
var str = "@@@hello@hi@@";
var match = str.match(/^@+/g)[0];
var new_str = str.replace(match, "");
var diff_count = str.length-new_str.length;
var new_sub_str = Array(diff_count+1).join("%")
var mystr = new_sub_str + new_str;
This solution does give me the intended output, but I am worried about the performance.
Is there any better way to achieve this ?
I am trying to replace a string starting with a specific symbol '@' with the symbol '%', but the condition is that the symbol should be at the start of the string.
For eg.
@@@hello@hi@@
should be replaced by
%%%hello@hi@@
I have e up with the regex that matches the starting '@' symbols, but I am able to replace it only once, instead of replacing it with the number of times it matched.
The code is
var str = "@@@hello@hi@@";
var exp = new RegExp('^@+', 'g');
var mystr = str.replace(exp, '%');
But, it outputs
%hello@hi@@
But, the intended output is
%%%hello@hi@@
My current solution is something like this:
var str = "@@@hello@hi@@";
var match = str.match(/^@+/g)[0];
var new_str = str.replace(match, "");
var diff_count = str.length-new_str.length;
var new_sub_str = Array(diff_count+1).join("%")
var mystr = new_sub_str + new_str;
This solution does give me the intended output, but I am worried about the performance.
Is there any better way to achieve this ?
Share Improve this question asked Dec 25, 2015 at 15:42 adi rohanadi rohan 8063 gold badges11 silver badges27 bronze badges 2-
If your string is guaranteed to begin with
@@@
, you could just dovar result = str.replace(/^@+/g, '%%%');
? – An0nC0d3r Commented Dec 25, 2015 at 15:49 - No, that was just an example. So the number of occurences may change. – adi rohan Commented Dec 25, 2015 at 15:51
3 Answers
Reset to default 6You can use a callback function:
var mystr = '@@@hello@hi@@'.replace(/^@+/g, function(match) {
return Array(match.length + 1).join('%');
});
document.write(mystr);
The Array(n).join(s)
construction is simply a shorthand way of repeating the string s
n-1
times.
An interesting solution without regexp:
var mystr = '@@@@@hello@hi@@'.split('').map(function(item) {
if (item == '@' && !this.stop) {
return '%';
} else {
this.stop = true;
return item;
}
}, {}).join('');
console.log(mystr);
And an alternative:
var mystr = Array.prototype.map.call('@@@@@hello@hi@@', function(item) {
if (item == '@' && !this.stop) {
return '%';
} else {
this.stop = true;
return item;
}
}, {}).join('');
console.log(mystr);
You can do it without a callback function as replacement with this pattern:
if (mystr.charAt(0)=='@')
mystr = mystr.replace(/@((?=@)|.*)/g, '%%$1');
Obviously, if you already know that the first character is always a @, remove the if condition.
If your string has newlines replace the dot with [^]
or [\s\S]
.
本文标签: javascriptReplace string starting with a symbol n timesStack Overflow
版权声明:本文标题:javascript - Replace string starting with a symbol n times - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742318078a2452218.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论