是 更好 , 更简单 ,还是 更高效 处理连词以获得相同结果的方法。 这将采用任何协调连接 列表类型 ,并获得预期的结果。
我有3个函数可以接受任何类型的and
- or
协调连接,并提供预期的输出。
function {
// Takes a sentence
// Replaces `\r` `\n` `\t` and `\s\s` with ` `
// Figures out which part of the sentence is the conjunction part
// Places the conjunction in an array call foundConjunction = []
// Nothing to see here - (Keep Scrolling)
}
第一个功能
计算我正在寻找的数量... 将来可能派上用场 ...如果我想知道有多少 单词或短语 需要处理
示例:
4 ,
告诉我,如果用户使用, or
,则他/她想要选择5个项目。
String.prototype.Count = function(find) {
let Key = find.test(this)
if (Key) {
return this.match(find).length
}
else {return 0}
}
第二功能
选择一个随机数 - 用于存在, or
- 或 - word or another word
function rand(min, max, interval) {
if (typeof(interval) === 'undefined') interval = 1
let r = Math.floor(Math.random() * (max - min + interval) / interval)
return r * interval + min
//var a = rand(0,2)
//var b = rand(4,6,0.0000001)
}
第三功能
弄清楚你想要什么。
String.prototype.conjunctionProcessing = function () {
let andKey = /,?(\sand\s).*$/g.test(this)
let orKey = /,(\sor\s).*$/g.test(this)
if (andKey===true&&orKey===false) {
let a = this.replace(/,?\sand/g, ',').split(/,\s/g)
let orKey = /\sor\s/g.test(a)
if (orKey) {
for (var i=0; i<a.length; i++) {
let b = a[i]
let orKey = /\sor\s/g.test(b)
if (orKey) {
let c = b.split(' or ');
let d = rand(0, c.length - 1);
a[i] = c[d];
}
}
}
return a
}
if (orKey) {
let a = this.replace(/,\sor?/g, ',').split(/,\s/g)
let orKey = /\sor\s/g.test(a)
if (orKey) {
for (var i=0; i<a.length; i++) {
let b = a[i]
let orKey = /\sor\s/g.test(b)
if (orKey) {
let c = b.split(' or ');
let rIndex = rand(0, c.length - 1);
a[i] = c[rIndex];
}
}
}
let rIndex = rand(0, a.length - 1);
let chosen1 = a[rIndex];
let andKey = /\sand\s/g.test(chosen1);
if (andKey) {return chosen1.split(/\sand\s/g)}
else {return chosen1};
}
}
处理时间
// Try any of these to see the outcome
// width and height
// width or height
// depth, width and height
// blue, width, and height
// gold, teal, or green
// hamster, cat or dog, horse and cow
// hamster, cat or dog, horse or cow, and mouse
// iPad and iPhone, Galaxy Tab and Galaxy Note 8, or Surface Tablet and Windows Phone
foundConjunction = ["blue, black or white, width and height, or background color"]
s1 = foundConjunction[0];
s2 = s1.conjunctionProcessing()
console.log(s2)
String.prototype.Count = function(find) {
let Key = find.test(this)
if (Key) {
return this.match(find).length
} else {
return 0
}
}
function rand(min, max, interval) {
if (typeof(interval) === 'undefined') interval = 1
let r = Math.floor(Math.random() * (max - min + interval) / interval)
return r * interval + min
//var a = rand(0,2)
//var b = rand(4,6,0.0000001)
}
String.prototype.conjunctionProcessing = function() {
let andKey = /,?(\sand\s).*$/g.test(this)
let orKey = /,(\sor\s).*$/g.test(this)
if (andKey === true && orKey === false) {
let a = this.replace(/,?\sand/g, ',').split(/,\s/g)
let orKey = /\sor\s/g.test(a)
if (orKey) {
for (var i = 0; i < a.length; i++) {
let b = a[i]
let orKey = /\sor\s/g.test(b)
if (orKey) {
let c = b.split(' or ');
let d = rand(0, c.length - 1);
a[i] = c[d];
}
}
}
return a
}
if (orKey) {
let a = this.replace(/,\sor?/g, ',').split(/,\s/g)
let orKey = /\sor\s/g.test(a)
if (orKey) {
for (var i = 0; i < a.length; i++) {
let b = a[i]
let orKey = /\sor\s/g.test(b)
if (orKey) {
let c = b.split(' or ');
let rIndex = rand(0, c.length - 1);
a[i] = c[rIndex];
}
}
}
let rIndex = rand(0, a.length - 1);
let chosen1 = a[rIndex];
let andKey = /\sand\s/g.test(chosen1);
if (andKey) {
return chosen1.split(/\sand\s/g)
} else {
return chosen1
};
}
}
foundConjunction = ["blue, black or white, width and height, or background color"]
s1 = foundConjunction[0];
s2 = s1.conjunctionProcessing()
console.log(s2)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>