Giả sử chuỗi có chuỗi ký tự đặc biệt:
var fullName=" John <----> Smith ";
Để tách thành chuỗi con, dùng regex và split(). Cú pháp:
var anyVariableName=(/\s*<---->\s*/g); var anyVariableName=yourVariableName.trim().split(yourVariableName);
Mã hoàn chỉnh:
Ví dụ
var fullName=" John <----> Smith "; console.log("The Value="+fullName); var regularExpression=(/\s*<---->\s*/g); var seprateName=fullName.trim().split(regularExpression); console.log(seprateName);Chạy: node fileName.js. File demo39.js.
Kết quả
PS C:\Users\Amit\JavaScript-code> node demo39.js The Value= John <----> Smith [ 'John', 'Smith' ]