英文:
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 = '23456\n734526754'
console.log(str)
str = str.match(/.{2}/g).join(' ');
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);
解决方案详细信息:
- 表达式 .{2} 匹配两个字符
- $1 用于在每个2个字符组之后添加空格
- g 用于匹配所有值
- trim 方法从字符串中移除任何前导和尾随空格。
英文:
You need to update your code.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let str = '234562\n7345267542';
str = str.replace(/(.{2})/g, "$1 ").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, '$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'
<!-- 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 = '23456\n734526754';
var output = str.replace(/(\S{1,2})(?=\S)/g, "$1 ");
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论