在字符串中给符号数组留出空间

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

Give space to array of signs in the string

问题

考虑到每个标点符号始终只出现在字符串的开头或结尾,我们希望在每个标点符号之前或之后添加一个空格,使其成为一个单词:

所需的结果已经在注释中给出,但是我的函数存在问题,如你所见:

modify('you always have to wake up?'); // you always have to wake up ? 

modify('...you always have to wake up'); // ... you always have to wake up

modify('...you always have to wake up?!'); // ... you always have to wake up ?!

function modify(string) {
    const sgins = ['?', '!', '...', '?!', '!?', '!!', '.'];
    for (let a = 0; a < sgins.length; a++) {
        const split = string.split(sgins[a]);
        string = string.replaceAll(sgins[a], ` ${sgins[a]} `).trim();
    }
    console.log(string);
}

你会如何解决这个问题?

英文:

considering each sign always in the start or end of the string only, we want to give each sgin a space before or after so as a word:

The desired result is commented, My function has an issue as you see:

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

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

modify(&#39;you always have to wake up?&#39;); // you always have to wake up ? 

modify(&#39;...you always have to wake up&#39;); // ... you always have to wake up

modify(&#39;...you always have to wake up?!&#39;); // ... you always have to wake up ?!

function modify(string) {
	const sgins = [&#39;?&#39;, &#39;!&#39;, &#39;...&#39;, &#39;?!&#39;, &#39;!?&#39;, &#39;!!&#39;, &#39;.&#39;];
  for (let a = 0; a &lt; sgins.length; a++) {
    const split = string.split(sgins[a]);
    string = string.replaceAll(sgins[a], ` ${sgins[a]} `).trim();
  }
  console.log(string);
}

<!-- end snippet -->

How would you do this?

答案1

得分: 1

你可以使用简单的正则表达式来实现:

string.replace(/[$?!]+/g, '$& ');

这个正则表达式的意思是将连续的$?!字符替换为这些字符加上一个空格,放在字符串的开头和结尾。

如果你需要精确的前缀,你可以构建一个正则表达式:

function modify(string) {
  const signs = ['?', '!', '...', '?!', '!?', '!!', '.'];
  const options = signs.sort((a, b) => b.length - a.length).map(prefix => [...prefix].map(c => '\\' + c).join('')).join('|');
  string = string.replace(new RegExp('^(' + options + ')'), '$1 ').replace(new RegExp('(' + options + ')$'), ' $1');
  console.log(string);
}

这个函数会根据给定的前缀列表构建一个正则表达式,并将字符串中符合前缀的部分替换为这些前缀加上一个空格,放在字符串的开头和结尾。

英文:

You could use a simple regex:

string.replace(/$[?!.]+/, &#39;$&amp; &#39;);

Means replace all continuous ?!. characters with the characters plus a space in the beginning of the string. The same for the end.

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

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

modify(&#39;you always have to wake up?&#39;); // you always have to wake up ? 

modify(&#39;...you always have to wake up&#39;); // ... you always have to wake up

modify(&#39;...you always have to wake up?!&#39;); // ... you always have to wake up ?!

function modify(string) {
   string = string.replace(/^[?!.]+/, &#39;$&amp; &#39;).replace(/[?!.]+$/, &#39; $&amp;&#39;);
   console.log(string);
}

<!-- end snippet -->

If you need exact prefixes you can build the regexp:

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

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

modify(&#39;you always have to wake up?&#39;); // you always have to wake up ? 

modify(&#39;...you always have to wake up&#39;); // ... you always have to wake up

modify(&#39;...you always have to wake up?!&#39;); // ... you always have to wake up ?!

function modify(string) {

  const sgins = [&#39;?&#39;, &#39;!&#39;, &#39;...&#39;, &#39;?!&#39;, &#39;!?&#39;, &#39;!!&#39;, &#39;.&#39;];

  const options = sgins.sort((a, b) =&gt; b.length - a.length).map(prefix =&gt; [...prefix].map(c =&gt; &#39;\\&#39; + c).join(&#39;&#39;)).join(&#39;|&#39;);

  string = string.replace(new RegExp(&#39;^(&#39; + options + &#39;)&#39;), &#39;$1 &#39;).replace(new RegExp(&#39;(&#39; + options + &#39;)$&#39;), &#39; $1&#39;);
  
  console.log(string);
}

<!-- end snippet -->

答案2

得分: 1

首先,老实说,我不会使用split()。我会这样写,但这不是最优化的代码。这只适用于某些情况,我不得不替换一些数组的符号才能正常工作。如果你想要完美的代码,那么你需要为许多不同的情况编写程序,所以代码会稍微长一些。例如,如果已经有空格了怎么办?等等。

function modify(string) {
  let didEnd = false;
  let didStart = false;
    
  const sgins = ['?!', '!?', '!!', '!', '?', '...', '.'];
  for (let a = 0; a < sgins.length; a++) {
    if (string.startsWith(sgins[a]) && !didStart) {
      string = string.replace(sgins[a], `${sgins[a]} `);
      didStart = true;
    }
    if (string.endsWith(sgins[a]) && !didEnd) {
      string = string.replace(sgins[a], ` ${sgins[a]}`);
      didEnd = true;
    }
  }
  console.log(string);
}

modify('you always have to wake up?');
modify('...you always have to wake up'); 
modify('...you always have to wake up?');

希望能帮到你!

英文:

First off I would not use split() to be honest with You. I would write this like that, but this is not optimal code. This is not for all cases, I had to replace some signs of Your array to work. If You want to make it perfect, then you have to program this for a lot of different cases so code would be a little bigger. Cause for example what if there already is space? ETC.

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

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

function modify(string) {
  let didEnd = false;
  let didStart = false;
    
  const sgins = [&#39;?!&#39;, &#39;!?&#39;, &#39;!!&#39;, &#39;!&#39;, &#39;?&#39;, &#39;...&#39;, &#39;.&#39;];
  for (let a = 0; a &lt; sgins.length; a++) {
    if (string.startsWith(sgins[a]) &amp;&amp; !didStart) {
      string = string.replace(sgins[a], `${sgins[a]} `);
      didStart = true;
    }
    if (string.endsWith(sgins[a]) &amp;&amp; !didEnd) {
      string = string.replace(sgins[a], ` ${sgins[a]}`);
      didEnd = true;
    }
  }
  console.log(string);
}

modify(&#39;you always have to wake up?&#39;);
modify(&#39;...you always have to wake up&#39;); 
modify(&#39;...you always have to wake up?!&#39;);

<!-- end snippet -->

答案3

得分: 0

作为替代方案,您可以使用具有全局匹配 /g 和 2 个捕获组的模式。

在回调函数中,检查第 1 组或第 2 组,以添加或追加一个空格。

const regex = /^([?!.]+)|([?!.]+)$/g;

modify('you always have to wake up?'); // you always have to wake up ? 
modify('...you always have to wake up'); // ... you always have to wake up
modify('...you always have to wake up?!'); // ... you always have to wake up ?!

function modify(string) {
  const regex = /^([?!.]+)|([?!.]+)$/g;
  console.log(string.replace(regex, (_, g1, g2) => g1 ? g1 + " " : " " + g2));
}

希望对您有所帮助!

英文:

As an alternative, you could use a pattern with a global match /g with 2 capture groups.

In the callback check for group 1 or group 2 to prepend or append a space.

const regex = /^([?!.]+)|([?!.]+)$/g;

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

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

modify(&#39;you always have to wake up?&#39;); // you always have to wake up ? 
modify(&#39;...you always have to wake up&#39;); // ... you always have to wake up
modify(&#39;...you always have to wake up?!&#39;); // ... you always have to wake up ?!

function modify(string) {
  const regex = /^([?!.]+)|([?!.]+)$/g;
  console.log(string.replace(regex, (_, g1, g2) =&gt; g1 ? g1 + &quot; &quot; : &quot; &quot; + g2));
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月27日 15:22:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777349.html
匿名

发表评论

匿名网友

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

确定