[英]Treat phrase in quotes as a word in javascript
I'm trying to find a way to spilt a string into single words OR phrases where the phrase is enclosed in quotes. 我正试图找到一种方法将字符串溢出成单个单词或短语,其中短语用引号括起来。 For example:
例如:
javascript 'sql server' vbscript javascript的sql server'vbscript
would be split into: 会被分成:
I got help yesterday with this question which splits the string into single words using 我昨天得到了这个问题的帮助, 该问题将字符串分成单个单词使用
/[-\w]+/g
I've been wrestling with the dark art of regular expressions and found this question which does a similar thing in php but even with the explanation it still doesn't make a great deal of sense to me (and doesn't work when I copy/paste into javascript!) 我一直在与正则表达式的黑暗艺术搏斗,并发现这个问题在PHP中做了类似的事情,但即使有解释它仍然没有给我很大的意义(并且当我复制时不起作用/粘贴到javascript!)
/"(?:\\\\.|[^\\\\"])*"|\S+/
Any help appreciated even a pointer to an easy to understand guide to regular expressions! 任何帮助甚至赞赏指向易于理解的正则表达式指南!
You should try the following: 您应该尝试以下方法:
/([-\w]+)|'([^']*)'/g
However, this is will fail if you have any form of single-quotes inside your quoted strings, that means, you cannot escape quotes with this construct. 但是,如果在引用的字符串中有任何形式的单引号,这将失败,这意味着,您无法使用此构造转义引号。
try this: 尝试这个:
var pattern = /('[^']+'|[-\w]+)/g;
var text = "javascript 'sql server' vbscript";
console.log(text.match(pattern));
也许你可以尝试这个var text = ... var split = function(tx) {return tx.split("\\'", }
这应该用引号将它拆分。然后只需用长度来计算它,就像所有偶数索引是引号。然后加入奇数索引并进行split(" ")
得到单词
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.