删除两个字符串中不存在的字符

发布于 2025-02-09 19:23:24 字数 692 浏览 3 评论 0原文

我有两个字符串,一个是一个可以更改的掩码,另一个是用户输入。

我需要保持口罩格式并删除我有两个字符串中不

rawValue: "25155122"
value: "2 5155 1220 0000" 

 newValue = "2 5155 122" <= 
the zeros are removed as they are a mask and user has not typed in the rest of the numbers yet

存在

const strSet = str => new Set(str.split(''));
const setDiff = (a, b) => new Set(Array.from(a).filter(item => !b.has(item)));
const prune = (str, set) => str.split('').filter(x => set.has(x))
  .join('');


  const diff = setDiff(strSet(rawValue), strSet(value));
  const a1 = prune(value, diff);
  const b1 = prune(rawValue, diff);

数字

I have two strings, one is a mask which can change and the other one is a users input.

I need to keep the mask format and remove numbers that are not present in both strings

rawValue: "25155122"
value: "2 5155 1220 0000" 

I have for example these two strings, the expected outcome would be this:

 newValue = "2 5155 122" <= 
the zeros are removed as they are a mask and user has not typed in the rest of the numbers yet

This is what i've tried so far:

const strSet = str => new Set(str.split(''));
const setDiff = (a, b) => new Set(Array.from(a).filter(item => !b.has(item)));
const prune = (str, set) => str.split('').filter(x => set.has(x))
  .join('');


  const diff = setDiff(strSet(rawValue), strSet(value));
  const a1 = prune(value, diff);
  const b1 = prune(rawValue, diff);

Thanks for the help in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骷髅 2025-02-16 19:23:24

不确定您到底想要什么,但希望这能解决您的问题...

const rawValue = "25155122"
const value = "2 5155 1220 0000" 

//convert string value of rawValue into an array
const rawValueArray = rawValue.split("");

//converts string value of Value into an array
const valArray = value.split("");

//filters out array value in VALUE that's not included in RAWVALUE
const newArray = valArray.filter(val => rawValueArray.includes(val));

console.log("new_value_without_spaces", newArray.join(""))

//filters out array value in VALUE that's not included in RAWVALUE and also ignores the spaces
const newArrayWithSpaces = valArray.filter(val => val === " " || rawValueArray.includes(val));

console.log("new_value_with_spaces", newArrayWithSpaces.join(""))

Not certainly sure what exactly you want, but hopefully this resolves your issue...

const rawValue = "25155122"
const value = "2 5155 1220 0000" 

//convert string value of rawValue into an array
const rawValueArray = rawValue.split("");

//converts string value of Value into an array
const valArray = value.split("");

//filters out array value in VALUE that's not included in RAWVALUE
const newArray = valArray.filter(val => rawValueArray.includes(val));

console.log("new_value_without_spaces", newArray.join(""))

//filters out array value in VALUE that's not included in RAWVALUE and also ignores the spaces
const newArrayWithSpaces = valArray.filter(val => val === " " || rawValueArray.includes(val));

console.log("new_value_with_spaces", newArrayWithSpaces.join(""))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文