正則表達式在線替換工具

正則表達式

Flags(第 1部分)

Flags(第 2部分)


替換為


關於正則表達式在線替換工具:

這個正則表達式在線替換工具可以幫助您替換字符串中符合您的正則化表達式部分.

cartoon text editor

鏈接:

Javascript RegExp手冊: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

Python re庫: https://docs.python.org/3/library/re.html

維基百科 (正則表達式): https://en.wikipedia.org/wiki/Regular_expression

用Javascript替換字符串中符合正則表達式的部分:

  1. var regex_replacer = /test/gi;
  2. var test_string = "This is a Test string.";
  3. var replace_to_str = "Result";
  4. var result = test_string.replace(regex_replacer,replace_to_str,test_string);
  5. console.log(result)
  6.  
  7. -------------------
  8. > "this is a Result string."

用Python替換字符串中符合正則表達式的部分:

  1. import re
  2.  
  3. regex_replacer = re.compile("test", re.IGNORECASE)
  4. test_string = "This is a Test string."
  5. replace_to = "result"
  6. result = regex_replacer.sub(replace_to, test_string)
  7. print(result)