简体   繁体   English

将引号中的短语视为javascript中的单词

[英]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: 会被分成:

  • javascript JavaScript的
  • sql server sql server
  • vbscript VBScript中

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.

相关问题 如何在 JavaScript 中的文本区域中选择单词或短语? - How to select a word or a phrase in a text area in JavaScript? 使用javascript计算短语中每个单词的出现次数 - Count the occurrence of each word in a phrase using javascript JavaScript RegEx排除某些单词/词组? - JavaScript RegEx excluding certain word/phrase? 使用 javascript 检查单词是否包含在引号内 - Check if a word is enclosed within quotes using javascript 如何从javascript中的单词中间删除双引号 - How to remove double quotes from a middle of a word in javascript Javascript正则表达式在没有引号的情况下按单词边界拆分字符串 - Javascript Regex to Split a String by Word Boundary when not in quotes 我想在javascript中的每个单词上添加单引号和逗号 - I want to add a single quotes and coma on every word in javascript 将单词的单数和复数视为一个 - Treat singular and plural of a word as one 使用 CSS 或 JavaScript 来识别一组单词或短语并仅对其进行不同的格式设置(例如颜色、大小)? - Using CSS or JavaScript to identify a set word or phrase and format only it differently (eg.colour, size)? 说消息 只给我一个短语的第一个单词,JavaScript - Discord.js - say message Only send me the first word of a phrase, JavaScript - Discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM