简体   繁体   English

在每个单词之前添加“@”+每个单词之间的一个空格(JavaScript)?

[英]Add "@" before every word + ONE space between every word (JavaScript)?

I put together a code, in the input field one writes words and the output shows same words but with @ attached and have one space in between each words.我把一个代码放在一起,在输入字段中一个写单词,输出显示相同的单词,但附加了@,每个单词之间有一个空格。 The code I have so far is semi-functional, it kinda does what I want to do, but with some issues.到目前为止,我拥有的代码是半功能的,它可以做我想做的事情,但有一些问题。

I need some help with a few things:我需要一些帮助:

1. I need to make sure that the ouput always has one space in between characters. 1.我需要确保输出的字符之间总是有一个空格。 If a user inputs ( types or pastes ) a sentence with MORE THAN ONE "space", the output shrinks it down to one space.如果用户输入(键入粘贴)一个包含多个“空格”的句子,则输出会将其缩小为一个空格。 As you can see:如你看到的:

Currently if you input: tomato, orange, (double space) apple, it will output @tomato @orange @ @apple.目前如果你输入:tomato, orange, (double space) apple,它会输出@tomato @orange @@ apple。

I want it to the output be: @tomato @orange @apple (remove the extra @, that was inserted due to double space)我希望它的输出是:@tomato @orange @apple(删除额外的@,由于双倍空格而插入)

2. When one deletes everything typed in the input field, the output still shows "@". 2.删除输入字段中输入的所有内容时,输出仍显示“@”。 I want the output be blank just like input if one deletes the input field.如果删除输入字段,我希望输出就像输入一样空白。

Here is the code:这是代码:

 function atPrefix (text) { return text .split(' ') .map(character => '@' + character) .join(' ') }
 <textarea id="specialInput" type="text" oninput="document.querySelector('#output').innerText = atPrefix(this.value)"></textarea> <p> <textarea id="output"> </textarea> </p>

Add a filter() function to filter out any token that is only made of spaces:添加一个filter()函数来过滤掉任何仅由空格组成的标记:

 function atPrefix (text) { return text .split(' ') .filter(token => token.trim() !== '') .map(token => '@' + token) .join(' ') }
 <textarea id="specialInput" type="text" oninput="document.querySelector('#output').innerText = atPrefix(this.value)"></textarea> <p> <textarea id="output"> </textarea> </p>

Something like this should work这样的事情应该工作

function atPrefix (text) {
  if(text) {
     return text
       .replace(/\s\s+/g, ' ')
       .split(' ')
       .map(character => '@' + character)
       .join(' ')
   } else {  return text }  
}

So basically you are checking if text is not an empty string, and replacing multiple space characters with single space through regex.所以基本上你正在检查文本是否不是空字符串,并通过正则表达式用单个空格替换多个空格字符。

your code is good but after the split just use then array.filter(e=>!!e);您的代码很好,但在拆分后只需使用 array.filter(e=>!!e);

 function atPrefix (text) { return text.split(' ') .filter(word => !!word) .map(word => '@' + word.trim()) .join(' ') }
 <textarea id="specialInput" type="text" oninput="document.querySelector('#output').innerText = atPrefix(this.value)"></textarea> <p> <textarea id="output"> </textarea> </p>

You can find an explanation for this regex here: https://regexr.com/4qgs7您可以在此处找到此正则表达式的解释: https : //regexr.com/4qgs7

 function atPrefix (text) { return text.replace(/\\b(\\w+)\\b/gm,"@$1"); }
 <textarea id="specialInput" type="text" oninput="document.querySelector('#output').innerText = atPrefix(this.value)"></textarea> <p> <textarea id="output"> </textarea> </p>

using regular express to replace ",?\\s+" with " @" and prefix with leading @;使用正则表达式将 ",?\\s+" 替换为 " @" 并以 @ 开头; Do the string manipulation only when it's not blank.仅当它不为空时才进行字符串操作。 this will address your second problem.这将解决您的第二个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM