简体   繁体   English

根据单词拆分字符串并使用 `^` 符号突出显示部分

[英]split string based on words and highlighted portions with `^` sign

I have a string that has highlighted portions with ^ sign:我有一个用^符号突出显示部分的字符串:

const inputValue = 'jhon duo ^has a car^ right ^we know^ that';

Now how to return an array which is splited based on words and ^ highlights, so that we return this array:现在如何返回一个根据单词和^高亮拆分的数组,以便我们返回这个数组:

['jhon','duo', 'has a car', 'right', 'we know', 'that']

Using const input = inputValue.split('^');使用const input = inputValue.split('^'); to split by ^ and const input = inputValue.split(' ');^const input = inputValue.split(' ');拆分to split by words is not working and I think we need a better idea.按文字拆分是行不通的,我认为我们需要一个更好的主意。

How would you do this?你会怎么做?

You can use match with a regular expression:您可以将match与正则表达式一起使用:

 const inputValue = 'jhon duo ^has a car^ right ^we know^ that'; const result = Array.from(inputValue.matchAll(/\^(.*?)\^|([^^\s]+)/g), ([, a, b]) => a || b); console.log(result);

  • \^(.*?)\^ will match a literal ^ and all characters until the next ^ (including it), and the inner part is captured in a capture group \^(.*?)\^将匹配文字^和所有字符,直到下一个^ (包括它),并且内部部分在捕获组中捕获
  • ([^^\s]+) will match a series of non-white space characters that are not ^ (a "word") in a second capture group ([^^\s]+)将匹配第二个捕获组中不是^ (“单词”)的一系列非空白字符
  • | makes the above two patterns alternatives: if the first doesn't match, the second is tried.使上述两种模式可供选择:如果第一个不匹配,则尝试第二个。
  • The Array.from callback will extract only what occurs in a capture group, so excluding the ^ characters. Array.from回调将仅提取捕获组中发生的内容,因此不包括^字符。

trincot's answer is good, but here's a version that doesn't use regex and will throw an error when there are mismatched ^ : trincot 的回答很好,但这里有一个不使用正则表达式的版本,当不匹配时会抛出错误^

 function splitHighlights (inputValue) { const inputSplit = inputValue.split('^'); let highlighted = true const result = inputSplit.flatMap(splitVal => { highlighted =;highlighted if (splitVal == '') { return []. } else if (highlighted) { return splitVal;trim(). } else { return splitVal.trim():split(' ') } }) if (highlighted) { throw new Error(`unmatched '^' char; expected an even number of '^' characters in input`); } return result. } console;log(splitHighlights('^jhon duo^ has a car right ^we know^ that')). console;log(splitHighlights('jhon duo^ has^ a car right we^ know that^')). console;log(splitHighlights('jhon duo^ has a car^ right ^we know^ that')). console;log(splitHighlights('jhon ^duo^ has a car^ right ^we know^ that'));

You can still use split() but capture the split-sequence to include it in the output.您仍然可以使用split()捕获拆分序列以将其包含在 output 中。
For splitting you could use *\^([^^]*)\^ *| +对于拆分,您可以使用*\^([^^]*)\^ *| + *\^([^^]*)\^ *| + to get trimmed items in the results. *\^([^^]*)\^ *| +在结果中得到修剪的项目。

 const inputValue = 'jhon duo ^has a car^ right ^we know^ that'; // filtering avoids empty items if split-sequence at start or end let input = inputValue.split(/ *\^([^^]*)\^ *| +/).filter(Boolean); console.log(input);

regex正则表达式 matches火柴
*\^ any amount of space followed by a literal caret任意数量的空格后跟一个文字插入符号
([^^]*) captures any amount of non -carets捕获任意数量插入符
\^ * literal caret followed by any amount of space文字插入符号后跟任意数量的空格
| + OR split at one or more spaces或者一个或多个空格处拆分

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

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