在 TypeScript 中,您可以使用正则表达式或字符串方法来读取特定的字符串。

方法 1:使用正则表达式

const str1 = 'KW232414115214';
const str2 = 'FE230822B00012-000000001866B5';

const regex1 = /KW\d+/;
const regex2 = /FE\d+/;

const match1 = str1.match(regex1);
const match2 = str2.match(regex2);

if (match1) {
  console.log(match1[0]); // KW232414115214
}

if (match2) {
  console.log(match2[0]); // FE230822B00012
}

方法 2:使用字符串方法

const str1 = 'KW232414115214';
const str2 = 'FE230822B00012-000000001866B5';

const substr1 = str1.substring(0, 14);
const substr2 = str2.substring(0, 14);

console.log(substr1); // KW232414115214
console.log(substr2); // FE230822B00012

无论使用哪种方法,都可以从给定的字符串中提取出所需的子字符串。

TypeScript 读取字符串中的特定内容:KW232414115214 和 FE230822B00012

原文地址: https://www.cveoy.top/t/topic/xcT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录