Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Cách lấy chuỗi con giữa hai ký tự giống nhau trong JavaScript

Giả sử chúng ta có chuỗi sau chứa dấu hai chấm:

var values = "This is:the first program in JavaScript:After that I will do Data Structure program";

Chúng ta cần lấy chuỗi con giữa hai dấu hai chấm. Kết quả mong đợi:

the first program in JavaScript

Sử dụng phương thức split().

Ví dụ

Mã minh họa:

var values = "This is:the first program in JavaScript:After that I will do Data Structure program"; 
var result = []; 
result = values.split(":"); 
console.log(result[1]);

Để chạy chương trình, sử dụng lệnh:

node tenFile.js

Tên file ở đây là demo273.js.

Kết quả

Đầu ra trên console:

PS C:\Users\Amit\javascript-code> node demo273.js
the first program in JavaScript