目的是從一個我們不知道它是否有變音符號但必須保留剩余字串(如果有的話)的任何和所有變音符號的阿拉伯字串中找到并洗掉一個起始詞(字串)。
在 StackOverflow 上從英文字串中洗掉第一個/起始詞有很多答案,但在 StackOverflow 上沒有找到解決此問題的現有解決方案,以保持阿拉伯字串在其原始形式中的平衡。
如果原始字串在處理之前被規范化(去除變音符號、tanween 等),那么回傳的剩余字串將是規范化字串的余額,而不是原始字串的余額。
例子。假設以下原始字串可以是以下任何一種形式(即相同的字串但不同的變音符號):
1. “?????? ????? ????? ????”
2. “??????? ?????? ?????? ????”
3. “???????? ??????? ??????? ????”
4. “?????????? ?????????? ?????????? ????”
現在我們要洗掉第一個/開始的單詞“??????”,僅當字串以這樣的單詞開頭(它確實如此),并回傳帶有原始變音符號的“原始”字串的余額。
當然,我們正在尋找沒有變音符號的單詞“??????”,因為我們不知道原始字串是如何使用變音符號格式化的。
因此,在這種情況下,每個字串的回傳余額必須是:
1. "????? ????? ????"
2. "?????? ?????? ????"
3. "??????? ??????? ????"
4. "?????????? ?????????? ????"
以下代碼適用于英文字串(還有許多其他解決方案),但不適用于上面解釋的阿拉伯字串。
function removeStartWord(string,word) {
if (string.startsWith(word)) string=string.slice(word.length);
return string;
}
上面的代碼使用了根據單詞長度從原始字串中切出第一個單詞的原理;這適用于英文文本。
對于阿拉伯字串,我們不知道原始字串的變音符號的形式,因此我們在原始字串中查找的單詞的長度將是不同的且未知的。
uj5u.com熱心網友回復:
盡管JavaScript不支持. _\p{Arabic}
一個基于類別的模式已經完全符合 OP 的要求/^[\p{L}\p{M}] \p{Z} /gmu......replace
從具有變音符號的阿拉伯字串中查找并洗掉第一個起始詞
模式……^[\p{L}\p{M}] \p{Z} 讀起來是這樣的……
^...從新行的開頭開始...[ ... ]...在串列中查找指定字符類的第一個字符...\p{L}...L來自任何語言的任何型別的 etter,\p{M}...或旨在與另一個字符組合的字符(例如重音符號、變音符號、封閉框等)
- ... 后跟
\p{Z}... 任何型別的空格或不可見分隔符中的至少一個。
console.log(`?????? ????? ????? ????
??????? ?????? ?????? ????
???????? ??????? ??????? ????
?????????? ?????????? ?????????? ????`.replace(/^[\p{L}\p{M}] \p{Z} /gmu, ''));
.as-console-wrapper { min-height: 100%!important; top: 0; }
編輯
由于現在很清楚 OP 真正想要什么,因此上述方法仍然存在,并且只是通過利用一個具有附加比較邏輯的函式來提升到一個新的水平,該replacer函式基于一個考慮了阿拉伯語和基本字母比較Intl.Collator的物件。
通過提供(除了'ar'本地人)一個具有基本敏感性的選項,整理者被初始化為最不嚴格的。因此,在通過整理者的compare方法比較兩個相似(但不完全相等)的字串時,例如'??????','??????????'將被認為是相等的,盡管后者具有(很多)變音符號。
證明/例子...
const baseLetterCollator = new Intl.Collator('ar', { sensitivity: 'base' } );
console.log(
"('?????? ????? ????? ????' === '?????????? ?????????? ?????????? ????') ?..",
('?????? ????? ????? ????' === '?????????? ?????????? ?????????? ????')
);
console.log('\n');
console.log(`new Intl.Collator()
.compare('?????? ????? ????? ????' ,'?????????? ?????????? ?????????? ????') === 0
?..`,
new Intl.Collator()
.compare('?????? ????? ????? ????' ,'?????????? ?????????? ?????????? ????') === 0
);
console.log(`new Intl.Collator('ar', { sensitivity: 'base' } )
.compare('?????? ????? ????? ????' ,'?????????? ?????????? ?????????? ????') === 0
?..`,
new Intl.Collator('ar', { sensitivity: 'base' } )
.compare('?????? ????? ????? ????' ,'?????????? ?????????? ?????????? ????') === 0
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
基于以上所述...最終解決方案...
function removeFirstMatchingWordFromEveryNewLine(search, multilineString) {
const baseLetterCollator
// - [ar]abic
// - base sensitivity
// ... only strings that differ in base letters compare as unequal.
= new Intl.Collator('ar', { sensitivity: 'base' } );
const replacer = word => {
return (baseLetterCollator.compare(search, word.trim()) === 0)
? '' // - remove the matching word (whitespace included).
: word; // - keep the word since there was no match.
}
const regXFirstLineWord = /^[\p{L}\p{M}] \p{Z} /gmu;
search = String(search).trim();
return String(multilineString).replace(regXFirstLineWord, replacer);
}
const sampleData = `?????? ????? ????? ????
??????? ?????? ?????? ????
???? ??????
???????? ??????? ??????? ????
?????????? ?????????? ?????????? ????`;
console.log('sampleData ...', sampleData);
console.log(
"removeFirstMatchingWordFromEveryNewLine('??????', sampleData) ...",
removeFirstMatchingWordFromEveryNewLine('??????', sampleData)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
uj5u.com熱心網友回復:
我看不出您的代碼有什么問題,但這是另一種方法:
function removeStartWord(string, word) {
return string.split(' ').filter((_word, index) => index !== 0 || _word.replace(/[^a-zA-Z?-?] /g, '') !== word).join(' ');
}
const sampleData = `?????? ????? ????? ????
??????? ?????? ?????? ????
???? ??????
???????? ??????? ??????? ????
?????????? ?????????? ?????????? ????`;
console.log('sampleData ...', sampleData);
console.log(
"removeStartWord(sampleData, '??????') ...",
removeStartWord(sampleData,'??????')
);
console.log(
"removeStartWord('??????', '?????? ????? ????? ????') ...",
removeStartWord('??????', '?????? ????? ????? ????')
);
console.log(
"removeStartWord('??????', '??????? ?????? ?????? ????') ...",
removeStartWord('??????', '??????? ?????? ?????? ????')
);
console.log(
"removeStartWord('??????', '???? ??????') ...",
removeStartWord('??????', '???? ??????')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/418266.html
標籤:
下一篇:目錄問題的不同htaccess
