javascript - Regex two string variables - Stack Overflow

Say I have two string variables:a = 'LOVE';b = '....';How do I use regex (or whate

Say I have two string variables:

a = 'LOVE';
b = '....';

How do I use regex (or whatever else is fastest) to bine a + b to make:

c = 'L.O.V.E.';

In my case, both strings are 4 characters long, always, and the second string is not a fixed character, I just made it a dot to make it look clearer on screen.

Say I have two string variables:

a = 'LOVE';
b = '....';

How do I use regex (or whatever else is fastest) to bine a + b to make:

c = 'L.O.V.E.';

In my case, both strings are 4 characters long, always, and the second string is not a fixed character, I just made it a dot to make it look clearer on screen.

Share Improve this question edited Jun 22, 2016 at 13:13 user1106925 asked Jun 22, 2016 at 12:38 cocacravecocacrave 2,6335 gold badges22 silver badges31 bronze badges 2
  • 2 The strings should match in length? Another question: the other string is usually a fixed character like "." or something similar? – n0m4d Commented Jun 22, 2016 at 12:45
  • 1 in my case, both strings are 4 characters long, always. and the second string is not a fixed character, I just made it a dot to make it look clearer on screen. – cocacrave Commented Jun 22, 2016 at 12:56
Add a ment  | 

6 Answers 6

Reset to default 10

You can simply loop through the longer string and in each iteration append one character from both strings to your resulting string. I don't think you need any regular expression there:

a = 'LOVE';
b = '....';

var binedString = ''; 
var largerLength = Math.max( a.length, b.length );

for( var i = 0; i < largerLength; i++ )
{
  binedString += a.charAt(i) + b.charAt(i);
}//for()

console.log( binedString );

The above code will work for strings of any length. In case, you know beforehand that both strings are exactly 4 characters long, I think the fastest and most efficient way would be:

a = 'LOVE';
b = '....';
var binedString = a.charAt[0] + b.charAt[0] + a.charAt[1] + b.charAt[1] + a.charAt[2] + b.charAt[2] + a.charAt[3] + b.charAt[3];
console.log( binedString );

You could use Array#reduce for it

var a = 'LOVE',
    b = '....';
    c = a.split('').reduce(function (r, v, i) {
        return r + v + b[i];
    }, '');

console.log(c);

How to bine a + b via regex:

var a = "LOVE", b = "....";

var result = a.replace(/./g, (match, i) => match + b[i]);

console.log(result);

There is no need of regex for your problem. You can simply do it with the help of for loop

a = 'LOVE';
b = '....';

var result = ''; 
var length = Math.max( a.length, b.length );
for( var i = 0; i <+ length-1; i++ )
{
    result = result + a.charAt(i); 
    result = result + b.charAt(i);
}

alert("Result of bined string is :"+ result);

You can use array functions on array likes (in this example strings) to iterate over it's items.

var a = 'LOVE',
  b = '....',
  c = Array.prototype.map
  .call(a, (v, i) => v + b[i]).join('');

console.log(c);

If your second string is always posed of dots, instead of repeating same characters in string, try something like this:

Using Delimiter

var a = "LOVE";
var delimeter = ".";
var result = a.split("").join(delimeter) + delimeter;
console.log(result)

Array convert + manual concatenation

As an alternate to string.charAt, you can try something like this:

Note: you should do a1[i] || "" for cases where value can be undefined. Also you should use .toString() to avoid cases where both values are numeric and result will be addition instead of concatenation.

var a = 'LOVE';
var b = '....';
var c = ",,,,,,,,,,,";
function mergeStr(a, b) {
  var a1 = a.split("");
  var b1 = b.split("");

  var len = Math.max(a.length, b.length)
  var r = "";
  for (var i = 0; i < len; i++) {
    r += (a1[i] || "").toString() + (b1[i] || "").toString();
  }
  
  return r;
}

console.log(mergeStr(a,b))
console.log(mergeStr(a,c))

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744290087a4566988.html

相关推荐

  • javascript - Regex two string variables - Stack Overflow

    Say I have two string variables:a = 'LOVE';b = '....';How do I use regex (or whate

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信