Move the first letter of each word to the end of it, then add “ay” to the end of the word.
Leave punctuation marks untouched.
pigIt('Pig latin is cool'); // igPay atinlay siay oolcay
pigIt('Hello world !'); // elloHay orldway !
library(tidyverse)
<- function(word) {
replace_rule str_c(str_sub(word, 2), str_sub(word, 1, 1), "ay")
}
<- function(string) {
pigIt %>%
string str_replace_all("\\w+", replace_rule)
}
library(testthat)
test_that("Sample Tests", {
expect_equal(pigIt("Pig latin is cool"), "igPay atinlay siay oolcay")
expect_equal(pigIt("Hello world !"), "elloHay orldway !")
})
#> Test passed 🌈
/**
* 修改/替换所有的单词,使用正则检测,避开标点符号
* @param {String} str
* @returns
*/
function pigIt(str) {
return str.replace(/\w+/g, word => word.slice(1) + word[0] + 'ay');
}