用特殊字元序列將字串分割成 JavaScript 中的兩對子字串?
假設我們有包含特殊字元序列的以下字串 -
var fullName=" John <----> Smith ";
要將上面字串分割成子字串,請使用正則表示式,然後 split()。語法如下 -
var anyVariableName=(/\s*<---->\s*/g); var anyVariableName=yourVariableName.trim().split(yourVariableName);
以下為完整的 JavaScript 程式碼 -
示例
var fullName=" John <----> Smith ";
console.log("The Value="+fullName);
var regularExpression=(/\s*<---->\s*/g);
var seprateName=fullName.trim().split(regularExpression);
console.log(seprateName);要執行上面程式,你需要使用以下命令 -
node fileName.js.
這裡,我的檔名是 demo39.js。
輸出
這將產生以下輸出 -
PS C:\Users\Amit\JavaScript-code> node demo39.js The Value= John <----> Smith [ 'John', 'Smith' ]
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP