在 JavaScript 中,在每2个字符之后添加空格,但忽略换行字符。

huangapple go评论95阅读模式
英文:

JavaScript add space to a string after 2 characters but ignore newline character

问题

I have to insert a space after every two characters in a string but by completely ignoring the newline character. I am able to do the spacing part but the problem comes with the newline as that is also counted as a character and when rendering the string it adds space at wrong positions.

let str = '23456\n734526754';
console.log(str)

str = str.match(/.{2}/g).join(' ');
console.log(str)

For the above code the output comes as

23 45 67 
3 45 26 75 3

What should be the ideal output is

23 45 67 
34 52 67 53

How can I ignore the newline character completely?

英文:

I have to insert a space after every two characters in a string but by completely ignoring the newline character. I am able to do the spacing part but the problem comes with the newline as that is also counted as a character and when rendering the string it adds space at wrong positions.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let str = &#39;23456\n734526754&#39;
console.log(str)

str = str.match(/.{2}/g).join(&#39; &#39;);
console.log(str)

<!-- end snippet -->

For the above code the output comes as

23 45 67 
3 45 26 75 3

What should be the ideal output is

23 45 67 
34 52 67 53

How can I ignore the newline character completely?

答案1

得分: 2

需要更新您的代码。

let str = '234562\n7345267542';
str = str.replace(/(.{2})/g, "$1 ").trim();
console.log(str);

解决方案详细信息:

  1. 表达式 .{2} 匹配两个字符
  2. $1 用于在每个2个字符组之后添加空格
  3. g 用于匹配所有值
  4. trim 方法从字符串中移除任何前导和尾随空格。
英文:

You need to update your code.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let str = &#39;234562\n7345267542&#39;;
str = str.replace(/(.{2})/g, &quot;$1 &quot;).trim();
console.log(str);

<!-- end snippet -->

Solution details :

1. expression .{2} matches two characters
2. $1 is for adding space after each group of 2 character
3. g is for match all values
4. trim method removes any leading and trailing whitespace from string

答案2

得分: 1

你可以在Javascript中使用这个间隔函数:

function spacer(s) {
   return s
     .replace(/^((?:.{2})*.)\n(.)(.*)/g, '$1$2\n$3')
     .replace(/.{2}(?=.)/g, '$& ');
}

console.log(spacer('23456\n734526754'));
// => '23 45 67\n34 52 67 54'

console.log(spacer('234562\n7345267542'));
// => '23 45 62\n73 45 26 75 42'

解决方案细节:

  • spacer函数使用了两个.replace调用
  • 第一个.replace找到奇数数量的字符后的\n,并使用3个捕获组将其移动到前面
  • 第二个.replace匹配一对任何字符(除了换行符)并在匹配后插入一个空格。
英文:

You may use this spacer function in Javascript:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function spacer(s) {
   return s
     .replace(/^((?:.{2})*.)\n(.)(.*)/g, &#39;$1$2\n$3&#39;)
     .replace(/.{2}(?=.)/g, &#39;$&amp; &#39;);
}

console.log(spacer(&#39;23456\n734526754&#39;));
//=&gt; &#39;23 45 67\n34 52 67 54&#39;

console.log(spacer(&#39;234562\n7345267542&#39;));
//=&gt; &#39;23 45 62\n73 45 26 75 42&#39;

<!-- end snippet -->

Solution Details:

  • spacer function user 2 .replace invocations
  • First .replace finds \n after matching odd number of characters and moves it one position ahead using 3 capture groups
  • Second .replace matches a pair of any characters (except line break) and inserts a space after the match

答案3

得分: 0

以下是使用单个正则表达式替换的方法:

var str = '23456\n734526754';
var output = str.replace(/(\S{1,2})(?=\S)/g, "$1 ");
console.log(output);

这里使用的正则表达式模式要求匹配:

  • (\S{1,2}) 匹配并捕获1或2个非空白字符
  • (?=\S) 断言后面跟随非空白字符(不在最后一个组后添加空格)

然后,我们使用 $1 替换,以在每个组后添加空格。

英文:

Here is an approach using a single regex replacement:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var str = &#39;23456\n734526754&#39;;
var output = str.replace(/(\S{1,2})(?=\S)/g, &quot;$1 &quot;);
console.log(output);

<!-- end snippet -->

The regex pattern used here says to match:

  • (\S{1,2}) match AND capture either 1 or 2 non whitespace characters
  • (?=\S) assert that non whitespace follows (do not add space after final group)

Then, we replace with $1 to effectively add a space after each group.

huangapple
  • 本文由 发表于 2023年2月10日 15:10:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定