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@@
@@@hello@hi@@
should be replaced by
应该被
%%%hello@hi@@
% % % hello@hi@@
I have come 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.
我想到了与开始的“@”符号匹配的regex,但是我只能替换它一次,而不能用它匹配的次数来替换它。
The code is
代码是
var str = "@@@hello@hi@@";
var exp = new RegExp('^@+', 'g');
var mystr = str.replace(exp, '%');
But, it outputs
但是,它输出
%hello@hi@@
% hello@hi@@
But, the intended output is
但是,预期的输出是
%%%hello@hi@@
% % % 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 ?
有更好的方法来实现这一点吗?
3 个解决方案
#1
6
You 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.
数组(n).join(s)结构只是简单地重复字符串s n-1次的一种简写方式。
#2
2
An interesting solution without regexp:
没有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);
#3
2
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条件。
If your string has newlines replace the dot with [^]
or [\s\S]
.
如果你的字符串有换行点替换[^]或[\ s \ s]。
#1
6
You 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.
数组(n).join(s)结构只是简单地重复字符串s n-1次的一种简写方式。
#2
2
An interesting solution without regexp:
没有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);
#3
2
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条件。
If your string has newlines replace the dot with [^]
or [\s\S]
.
如果你的字符串有换行点替换[^]或[\ s \ s]。