如何在 Javascript 保留 space 位置打乱句子?
How to scramble a sentence in Javascript preserving space location?
我 found an script 将打乱字符串:
String.prototype.shuffle = function () {
var a = this.split(""),
n = a.length;
for (var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
return a.join("");
}
使用此脚本,以下单词:
What is the difference in between 'Apache Environment', 'Environment' and 'PHP Variables'?
将随机洗牌到这个词:
ftewE'eim rasent VhiEAn'oeded ta ' mb one'ennfva'nbcr?n elcttpnP iaWePh'irH rshv ieinena,
但是,我想知道如何保留每个 space:
的原始位置
ftew E' eim rasentVhiE An 'oededt a'mbone 'ennfva'nbcr? nelcttpnPiaWe Ph' irHr shvieinena,
一个选项是创建一个随机字符数组(没有 spaces),然后使用替换非 space 的正则表达式对原始字符串调用 replace
在关联索引处的数组中具有项目的字符,在此过程中递增索引:
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
String.prototype.shuffle = function () {
// pass to shuffleArray an array of all non-whitespace characters:
const randomChars = shuffleArray([...this.replace(/\s+/g, '')]);
let index = 0;
// `\S` matches any non-whitespace character:
return this.replace(/\S/g, () => randomChars[index++]);
}
console.log(
`What is the difference in between 'Apache Environment', 'Environment' and 'PHP Variables'?`
.shuffle()
);
还要注意像 String.prototype
这样的改变内置对象通常被认为是非常糟糕的做法,并且会破坏东西;除非你正在填充官方的东西,否则最好使用独立函数:
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
function shuffle(str) {
const randomChars = shuffleArray([...str.replace(/\s+/g, '')]);
let index = 0;
return str.replace(/\S/g, () => randomChars[index++]);
}
console.log(shuffle(
`What is the difference in between 'Apache Environment', 'Environment' and 'PHP Variables'?`
));
如果 a[i]
和 a[j]
是空格,您也可以简单地检查您的函数:
const shuffleMeSoftly = function(str, breaker = ' ') {
var a = str.split(""),
n = a.length;
for (var i = n - 1; i > 0; i--) {
if (a[i] != breaker) {
var j = Math.floor(Math.random() * (i + 1));
if (a[j] != breaker) {
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
return a.join("");
}
console.log(shuffleMeSoftly('What is the difference in between \'Apache Environment\', \'Environment\' and \'PHP Variables\'?'))
我 found an script 将打乱字符串:
String.prototype.shuffle = function () {
var a = this.split(""),
n = a.length;
for (var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
return a.join("");
}
使用此脚本,以下单词:
What is the difference in between 'Apache Environment', 'Environment' and 'PHP Variables'?
将随机洗牌到这个词:
ftewE'eim rasent VhiEAn'oeded ta ' mb one'ennfva'nbcr?n elcttpnP iaWePh'irH rshv ieinena,
但是,我想知道如何保留每个 space:
的原始位置ftew E' eim rasentVhiE An 'oededt a'mbone 'ennfva'nbcr? nelcttpnPiaWe Ph' irHr shvieinena,
一个选项是创建一个随机字符数组(没有 spaces),然后使用替换非 space 的正则表达式对原始字符串调用 replace
在关联索引处的数组中具有项目的字符,在此过程中递增索引:
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
String.prototype.shuffle = function () {
// pass to shuffleArray an array of all non-whitespace characters:
const randomChars = shuffleArray([...this.replace(/\s+/g, '')]);
let index = 0;
// `\S` matches any non-whitespace character:
return this.replace(/\S/g, () => randomChars[index++]);
}
console.log(
`What is the difference in between 'Apache Environment', 'Environment' and 'PHP Variables'?`
.shuffle()
);
还要注意像 String.prototype
这样的改变内置对象通常被认为是非常糟糕的做法,并且会破坏东西;除非你正在填充官方的东西,否则最好使用独立函数:
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
function shuffle(str) {
const randomChars = shuffleArray([...str.replace(/\s+/g, '')]);
let index = 0;
return str.replace(/\S/g, () => randomChars[index++]);
}
console.log(shuffle(
`What is the difference in between 'Apache Environment', 'Environment' and 'PHP Variables'?`
));
如果 a[i]
和 a[j]
是空格,您也可以简单地检查您的函数:
const shuffleMeSoftly = function(str, breaker = ' ') {
var a = str.split(""),
n = a.length;
for (var i = n - 1; i > 0; i--) {
if (a[i] != breaker) {
var j = Math.floor(Math.random() * (i + 1));
if (a[j] != breaker) {
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
return a.join("");
}
console.log(shuffleMeSoftly('What is the difference in between \'Apache Environment\', \'Environment\' and \'PHP Variables\'?'))