SHARE
TWEET

Untitled

a guest May 22nd, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function symbolReplace (string, replaceValue) {
  2.     /** @type RegExp */
  3.     let rx = this;
  4.     if (rx == null || typeof rx !== 'object') throw new TypeError();
  5.     let S = `${string}`;
  6.     let lengthS = S.length;
  7.     let functionalReplace = typeof replaceValue === 'function';
  8.     if (!functionalReplace) {
  9.         replaceValue = `${replaceValue}`;
  10.     }
  11.     let global = !!rx.global;
  12.     let fullUnicode;
  13.     if (global) {
  14.         fullUnicode = !!rx.unicode;
  15.         rx.lastIndex = 0;
  16.     }
  17.     let results = [];
  18.     while (true) {
  19.         let result = rx.exec(S);
  20.         if (result === null) break;
  21.  
  22.         results.push(result);
  23.  
  24.         if (!global) break;
  25.  
  26.         let matchStr = result[0];
  27.         if (matchStr === '') {
  28.             let thisIndex = rx.lastIndex;
  29.             let nextIndex = advanceStringIndex(S, thisIndex, fullUnicode);
  30.             // fullUnicode が true かつ thisIndex と thisIndex + 1 が一つのサロゲートペアを指す場合、index + 2 を返します。
  31.             // それ以外の場合、index + 1 を返します。
  32.             rx.lastIndex = nextIndex;
  33.         }
  34.     }
  35.     let accumulatedResult = '';
  36.     let nextSourcePosition = 0;
  37.     for (let result of results) {
  38.         let nCaptures = result.length;
  39.         nCaptures = Math.max(nCaptures - 1, 0);
  40.         let matched = result[0];
  41.         let matchLength = matched.length;
  42.         let position = result.index | 0;
  43.         position = Math.max(Math.min(position, lengthS), 0);
  44.         let captures = [];
  45.         for (let n = 1; n <= nCaptures; ++n) {
  46.             let capN = result[n];
  47.             if (capN !== undefined) capN = `${capN}`;
  48.             captures.push(capN);
  49.         }
  50.         let replacement;
  51.         if (functionalReplace) {
  52.             let replacerArgs = [matched, ...captures, position, S];
  53.             replacement = Reflect.apply(replaceValue, undefined, replacerArgs);
  54.         } else {
  55.             replacement = GetSubstitution(matched, S, position, captures, replaceValue);
  56.             // 基本的には replaceValue を返す。実際は '$$' や '$1' などを解決する
  57.         }
  58.         if (position >= nextSourcePosition) {
  59.             accumulatedResult += S.substring(nextSourcePosition, position) + replacement;
  60.             nextSourcePosition = position + matchLength;
  61.         }
  62.         if (nextSourcePosition >= lengthS) return accumulatedResult;
  63.         return accumulatedResult + S.substring(nextSourcePosition);
  64.     }
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top