我正在嘗試撰寫一些代碼,但我嘗試過但不起作用,所以我有一個字串,我希望我的代碼在其中搜索模式。例如,如果“你好,這是一個測驗”,我想在這個字串中搜索“etet”模式和第一次迭代。在這種情況下,這將是:1 H é LLO,牛逼他是?小號牛逼“后,其他字符將被替換為“ - ”就像這樣:“ - é -----牛逼---- ------ e - t '。我試過正則運算式:
string = string.replace(/[^eEtT]/g, '-')
但它給出了每個字母,而不是唯一的模式。謝謝你的幫助。
uj5u.com熱心網友回復:
您可能會使用2個捕獲組,先捕獲e然后下一個t
在replace的回呼中,檢查是否有捕獲組。
(e)[^t]*(t)|.
正則運算式演示
替換為第 1 組和第 2 組,并重復添加_其間匹配的字符數。
const regex = /(e)[^t]*(t)|./gi;
const s = "hello, this is a test.";
const result = s.replace(regex, (m, g1, g2) =>
g1 ? g1 "-".repeat(m.length - 2) g2 : "-"
);
console.log(result);
uj5u.com熱心網友回復:
老實說,我認為這沒有實際用途,但如果你想要它,我想當然。像這樣的事情可能會有所幫助:
function stringSearch(string, search, caseSensitive=true) {
if(caseSensitive==false) {
string = string.toLowerCase();
search = search.toLowerCase();
}
let cache = ""; // this will store the characters we have found
let complete = false; // whether we found the search string or not
let pos = 0; // current position in search
for(i=0;i<string.length;i ) { // loop through every character in the string
if(string[i] == search[pos]) { // check if it matches the current character in the searchkey
cache = search[pos]; // if so, add the character to the cache
pos ; // add 1 to the position
if(cache == search) { // check if the cache is equal to the search string
complete = true; // set complete to true
break; // break the loop
}
}
}
return complete;
}
// examples:
console.log("'helolo' has 'hello': " stringSearch('helolo', 'hello')); // true
console.log("'helo' has 'hello': " stringSearch('helo', 'hello')) // false
console.log("'HELLO' has 'hello': " stringSearch('HELLO', 'hello')); // false
console.log("'HELLO' has 'hello' (incase sensitive): " stringSearch('HELLO', 'hello', false)); // true
它的作業方式非常簡單。它有一個快取變數。該變數將保存與搜索字串匹配的每個字符。
因此,它遍歷大字串中的每個字符。然后還有另一個變數用于存盤搜索字串中的當前位置。如果大字串中的當前字符與搜索字串中的當前字符匹配,則將其添加到快取變數并將其添加到搜索字串的位置。然后它運行一個 if 陳述句來查看快取是否等于搜索字串。如果是,則回傳 true。如果需要,您可以讓函式回傳快取而不是布林值。
如果你想要破折號,你可以使用這個代碼:
function stringSearch(string, search, caseSensitive=true) {
if(caseSensitive==false) {
string = string.toLowerCase();
search = search.toLowerCase();
}
let cache = ""; // this will store the characters we have found
let complete = false; // whether we found the search string or not
let returnValue = "";
let pos = 0; // current position in search
for(i=0;i<string.length;i ) { // loop through every character in the string
if(string[i] == search[pos]) { // check if it matches the current character in the searchkey
cache = search[pos]; // if so, add the character to the cache
returnValue = search[pos]; // add the character to the return value
pos ; // add 1 to the position
if(cache == search) { // check if the cache is equal to the search string
complete = true;
break; // break the loop
}
} else {
returnValue = "-"; // add a - to the return value
}
}
if(complete) {
return returnValue; // return the result
} else {
return;
}
}
// examples:
console.log("'helolo' has 'hello': " stringSearch('helolo', 'hello')); // true
console.log("'helo' has 'hello': " stringSearch('helo', 'hello')) // false
console.log("'HELLO' has 'hello': " stringSearch('HELLO', 'hello')); // false
console.log("'HELLO' has 'hello' (incase sensitive): " stringSearch('HELLO', 'hello', false)); // true
console.log("'hello, this is a test' has 'etet': " stringSearch("hello, this is a test", "etet"));
它的作業原理幾乎相同。唯一的區別是添加了一個 returnValue 變數,用于存盤破折號和文本。每次將字符添加到快取時,它也會添加到 returnValue。但是,如果一個值沒有添加到快取中,一個 - 會添加到 returnValue。
這沒有抓住中間的 't' 的原因是因為當它越過那個 't' 時,它仍在尋找一個 'e'。
uj5u.com熱心網友回復:
這個正則運算式:
/(^.*?)(e)(.*?)(t)(.*?)(e)(.*?)(t)(.*)/gm
(^.*?) 匹配字串的開頭,包含 0 個或多個任意字符,直到它到達...
(e) 一個 e,那么
(.*?) 0個或更多的任何字符,直到它到達
(t) 在。
等等...
匹配的組hello-this-is-the-test是:
0-1 h
1-2 e
2-6 llo-
6-7 t
7-16 his-is-th
16-17 e
17-18 -
18-19 t
19-22 est
https://regex101.com/r/FPRCT1/1
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/389353.html
標籤:javascript 正则表达式
上一篇:競爭正則運算式(競爭條件)
