开发者

Javascript Regex- replace sequence of characters with same number of another character

开发者 https://www.devze.com 2023-04-05 08:37 出处:网络
I\'m trying to replace part of a string with the same number of dummy characters in JavaScript, for example: \'==Hello==\' with \'==~~~~~==\'.

I'm trying to replace part of a string with the same number of dummy characters in JavaScript, for example: '==Hello==' with '==~~~~~=='.

This question has been answered using Perl and PHP, but I can't get it to work in JavaScript. I've been trying this:

txt=txt.replace(/(==)([^=]+)(==)/g, "$1"+Array("$2".length + 1).join('~')+"$3");

The pattern match works fine, but the replacement does not - the second part adds '~~' instead of the length of the pattern match. Putting the "$2" inside th开发者_运维问答e parentheses doesn't work. What can I do to make it insert the right number of characters?


Use a function for replacement instead:

var txt = "==Hello==";
txt = txt.replace(/(==)([^=]+)(==)/g, function ($0, $1, $2, $3) {
    return $1 + (new Array($2.length + 1).join("~")) + $3;
});

alert(txt);
//-> "==~~~~~=="


The issue with the expression

txt.replace(/(==)([^=]+)(==)/g, "$1"+Array("$2".length + 1).join('~')+"$3") 

is that "$2".length forces $2 to be taken as a string literal, namely the string "$2", that has length 2.

From the MDN docs:

Because we want to further transform the result of the match before the final substitution is made, we must use a function.

This forces evaluation of the match before the transformation.

With an inline function as parameter (and repeat) -- here $1, $2, $3 are local variables:

txt.replace(/(==)([^=]+)(==)/g, (_,$1,$2,$3) => $1+'~'.repeat($2.length)+$3);

txt = '==Hello==';

//inline function
console.log(
  txt.replace(/(==)([^=]+)(==)/g, (_, g1, g2, g3) => g1 + '~'.repeat(g2.length) + g3)
);


The length attribute is being evaluated before the $2 substitution so replace() won't work. The function call suggested by Augustus should work, another approach would be using match() instead of replace().

Using match() without the /g, returns an array of match results which can be joined as you expect.

txt="==Hello==";
mat=txt.match(/(==)([^=]+)(==)/);  // mat is now ["==Hello==","==","Hello","=="]
txt=mat[1]+Array(mat[2].length+1).join("~")+mat[3]; // txt is now "==~~~~~=="

You excluded the leading/trailing character from the middle expression, but if you want more flexibility you could use this and handle anything bracketed by the leading/trailing literals.

mat=txt.match(/(^==)(.+)(==$)/)


A working sample uses the following fragment:

var processed = original.replace(/(==)([^=]+)(==)/g, function(all, before, gone, after){
    return before+Array(gone.length+1).join('~')+after;
});

The problem in your code was that you always measured the length of "$2" (always a string with two characters). By having the function you can measure the length of the matched part. See the documentation on replace for further examples.

0

精彩评论

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