#String: Replace All by RegExp

#String: Replace All by RegExp

#String: Replace All by RegExp

translate #文字列: 正規表現で全置換

Replaces all strings that match the regular expression. For example, all Phone Numbers ‘\d{3}-\d{3}-\d{4}’ in a text string can be replaced with “999-999-9999”.

Auto Step icon
Configs for this Auto Step
StrConfA
A: Set Input Text *#{EL}
StrConfB1
B1: Set Regular Expression (eg: “\d{3}-\d{3}-\d{4}”) *#{EL}
BoolConfB2
B2: Case Sensitive or Case should be Ignored
StrConfB3
B3: Set Replacement String (eg: “999-999-9999”)#{EL}
SelectConfC
C: Select DATA to Output Text (update) *
SelectConfD1
D1: Select NUMERIC for Number of Text Lines (update)
Script (click to open)
// Script Example of Business Process Automation
// for 'engine type: 3' ("GraalJS standard mode")
// cf. 'engine type: 2' ("GraalJS Nashorn compatible mode") (renamed from "GraalJS" at 20230526)


//////// START "main()" /////////////////////////////////////////////////////////////////
main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const strInput       = configs.get       ( "StrConfA" );      // REQUIRED
  if( strInput     === "" ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {A: InputText} is empty \n" );
  }
const numInputLines  = strInput.split("\n").length;

const strRegExp      = configs.get       ( "StrConfB1" );     // REQUIRED
const boolIgnoreCase = configs.getObject ( "BoolConfB2" );    // TOGGLE
  // https://questetra.zendesk.com/hc/ja/articles/360024574471-R2300 "Boolean object"
const strReplacement = configs.get       ( "StrConfB3" );     // not required
const strPocketC     = configs.getObject ( "SelectConfC" );   // REQUIRED
const numPocketD1    = configs.getObject ( "SelectConfD1" );  // not required



//// == Data Retrieving / ワークフローデータの参照 ==
// (nothing)



//// == Calculating / 演算 ==
const regSearch  = boolIgnoreCase ?
                   new RegExp( strRegExp, 'ig' ) : new RegExp( strRegExp, 'g' );



//// == Data Updating / ワークフローデータへの代入 ==
/// ref) Retrieving / Updating from ScriptTasks
/// https://questetra.zendesk.com/hc/en-us/articles/360024574771-R2301
/// https://questetra.zendesk.com/hc/ja/articles/360024574771-R2301

if ( strPocketC !== null ){ 
  engine.setData( strPocketC, strInput.replaceAll ( regSearch, strReplacement ) );
}

if ( numPocketD1 !== null ){ 
  engine.setData( numPocketD1, new java.math.BigDecimal( numInputLines ) );
}

} //////// END "main()" /////////////////////////////////////////////////////////////////



/*
### NOTES
- When a Process reaches this [Automated Step], the replacement is automatically executed.
    - Matching strings within the input text are replaced.
- The number of lines in the input text can also be recorded.
- Regular Expressions
    - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions

### NOTES-ja
- この[自動工程]に案件が到達すると、「置換処理」が自動実行されます。
    - Inputテキスト内にある全ての「マッチ文字列」が置換されます。
- 入力Inputテキストの行数も記録可能です。
- 正規表現とは
    - https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Regular_expressions

### Test Data
```
- Tokyo Headquarters
    - Main Line: 03-1234-5678
    - Sales Department: 03-9876-5432
    - Human Resources: 03-1111-2222
    - General Affairs: +81-3-3333-4444
- Osaka Branch
    - Main Line: 06-1234-5678
    - Sales Team 1: 06-8765-4321
```

### APPENDIX
- 正規表現 設定例 / RegExp Example
    - 日本の郵便番号 / Japanese Postal Code
        - `\d{3}-\d{4}`
        - `[0-9]{3}-[0-9]{4}`
        - `(\d{3})-(\d{4})`
        - `(\d{3}-\d{4})`
        - `(\d{3}-\d{4})|(\d{7})`
        - `([0-9]{3}-[0-9]{4})|([0-9]{7})`
    - Email
        - `[\w_.+-]+@[\w-.]+[a-zA-Z]{2,}`
        - `[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-.]+[a-zA-Z]{2,}`
        - `[a-zA-Z0-9_.+-]+@([a-zA-Z0-9-.]+[a-zA-Z]{2,})`
        - `[a-zA-Z0-9_.+-]+@([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]*\.)+[a-zA-Z]{2,}`
        - `[a-zA-Z0-9_.+-]+@(([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]*\.)+[a-zA-Z]{2,})`
    - URL
        - `https?://[\w/:%#\$&\?\(\)~\.=\+\-]+`
    - ISO Date from 2024-12-15 to 2025-01-06
        - `(2024-12-1[5-9])|(2024-12-[2-3][0-9])|(2025-01-0[1-6])`
- 正規表現グループ / Groups in regular expressions
    - `(apple|orange)`
        - "apple" か "orange" のいずれか / Either "apple" or "orange"
    - 正規表現は複数のキャプチャグループを持つことができます。
        - キャプチャグループ内の左括弧と同じ順にある、配列要素に一致します。
    - A regular expression may have multiple capturing groups.
        - Matches to capturing groups in an array whose members are in the same order as the left parentheses
- 正規表現文字クラス / A character class
    - `[xyz]`
        - "x" か "y" か "z" の 一文字 / Any one of the enclosed characters, "x" "y" or "z"
    - `[a-c]`
        - "a" から "c" までのいずれか一文字 / Any one in the range "a" to "c"
    - `[^xyz]`
        - "x" でも "y" でも "z" でもない一文字 / Any one that is neither "x" nor "y" nor "z"
    - `\d`
        - 数字一文字 / Any digit / `[0-9]`
    - `\w`
        - 半角英数字一文字 / Any alphanumeric character / `[A-Za-z0-9_]`
    - `\t`
        - 水平タブ / A horizontal tab
    - `.`
        - 改行文字を除くあらゆる一文字 / Any single character except line terminators
- 正規表現アサーション言明 / Assertions
    - `^`
        - 先頭 / Beginning
    - `$`
        - 末尾 / End
    - `\b`
        - 区切り / A word boundary
    - `\B`
        - 区切り以外 / A non-word boundary
- 正規表現数量詞 / Quantifiers
    - `x*`
        - 直前アイテム "x" の0回以上の繰返 / The preceding item "x" 0 or more times
    - `x+`
        - 直前アイテム "x" の1回以上の繰返 / The preceding item "x" 1 or more times
    - `x?`
        - 直前アイテム "x" の0回か1回の出現 / The preceding item "x" 0 or 1 times
    - `x{n}`
        - 直前アイテム "x" のn回の出現 / "n" occurrences of the preceding item "x"
    - `x{n,m}`
        - 直前アイテム "x" がnからm回出現 / at least "n" and at most "m" occurrences
*/

Download

warning Freely modifiable JavaScript (ECMAScript) code. No warranty of any kind.
(Installing Addon Auto-Steps are available only on the Professional edition.)

Notes

Capture

Test Data

- Tokyo Headquarters
    - Main Line: 03-1234-5678
    - Sales Department: 03-9876-5432
    - Human Resources: 03-1111-2222
    - General Affairs: +81-3-3333-4444
- Osaka Branch
    - Main Line: 06-1234-5678
    - Sales Team 1: 06-8765-4321

Appendix

  • 正規表現 設定例 / RegExp Example
    • 日本の郵便番号 / Japanese Postal Code
      • \d{3}-\d{4}
      • [0-9]{3}-[0-9]{4}
      • (\d{3})-(\d{4})
      • (\d{3}-\d{4})
      • (\d{3}-\d{4})|(\d{7})
      • ([0-9]{3}-[0-9]{4})|([0-9]{7})
    • Email
      • [\w_.+-]+@[\w-.]+[a-zA-Z]{2,}
      • [a-zA-Z0-9_.+-]+@[a-zA-Z0-9-.]+[a-zA-Z]{2,}
      • [a-zA-Z0-9_.+-]+@([a-zA-Z0-9-.]+[a-zA-Z]{2,})
      • [a-zA-Z0-9_.+-]+@([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]*\.)+[a-zA-Z]{2,}
      • [a-zA-Z0-9_.+-]+@(([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]*\.)+[a-zA-Z]{2,})
    • URL
      • https?://[\w/:%#\$&\?\(\)~\.=\+\-]+
    • ISO Date from 2024-12-15 to 2025-01-06
      • (2024-12-1[5-9])|(2024-12-[2-3][0-9])|(2025-01-0[1-6])
  • 正規表現グループ / Groups in regular expressions
    • (apple|orange)
      • “apple” か “orange” のいずれか / Either “apple” or “orange”
    • 正規表現は複数のキャプチャグループを持つことができます。
      • キャプチャグループ内の左括弧と同じ順にある、配列要素に一致します。
    • A regular expression may have multiple capturing groups.
      • Matches to capturing groups in an array whose members are in the same order as the left parentheses
  • 正規表現文字クラス / A character class
    • [xyz]
      • “x” か “y” か “z” の 一文字 / Any one of the enclosed characters, “x” “y” or “z”
    • [a-c]
      • “a” から “c” までのいずれか一文字 / Any one in the range “a” to “c”
    • [^xyz]
      • “x” でも “y” でも “z” でもない一文字 / Any one that is neither “x” nor “y” nor “z”
    • \d
      • 数字一文字 / Any digit / [0-9]
    • \w
      • 半角英数字一文字 / Any alphanumeric character / [A-Za-z0-9_]
    • \t
      • 水平タブ / A horizontal tab
    • .
      • 改行文字を除くあらゆる一文字 / Any single character except line terminators
  • 正規表現アサーション言明 / Assertions
    • ^
      • 先頭 / Beginning
    • $
      • 末尾 / End
    • \b
      • 区切り / A word boundary
    • \B
      • 区切り以外 / A non-word boundary
  • 正規表現数量詞 / Quantifiers
    • x*
      • 直前アイテム “x” の0回以上の繰返 / The preceding item “x” 0 or more times
    • x+
      • 直前アイテム “x” の1回以上の繰返 / The preceding item “x” 1 or more times
    • x?
      • 直前アイテム “x” の0回か1回の出現 / The preceding item “x” 0 or 1 times
    • x{n}
      • 直前アイテム “x” のn回の出現 / ”n” occurrences of the preceding item “x”
    • x{n,m}
      • 直前アイテム “x” がnからm回出現 / at least “n” and at most “m” occurrences

See Also

Scroll to Top

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading