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 do var 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
Add a ment  | 

3 Answers 3

Reset to default 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.

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