admin管理员组文章数量:1401849
I'm receiving data in this form
('FCA',)
('JP NIC',)
('JP CHI',)
('2022-03-04T07:18:36.468Z',)
I want to clean up to remove Brackets, string quote and ma
FCA
JP NIC
JP CHI
2022-03-04T07:18:36.468Z
I'm trying to use substring()
But problem is number of characters in this data can be changed but these value are constant and I want to remove them ('',)
. How I can do this ?
I'm receiving data in this form
('FCA',)
('JP NIC',)
('JP CHI',)
('2022-03-04T07:18:36.468Z',)
I want to clean up to remove Brackets, string quote and ma
FCA
JP NIC
JP CHI
2022-03-04T07:18:36.468Z
I'm trying to use substring()
But problem is number of characters in this data can be changed but these value are constant and I want to remove them ('',)
. How I can do this ?
- 2 It would be better to clear up the issues you have with why your server is sending you terrible data. – Andy Commented Mar 4, 2022 at 7:35
4 Answers
Reset to default 2you can use regex and string.replace
string.replace(/[|&;$%@"<>()+,]/g, "");
just put whatever string you want to ignore inside the rectangular brackets, i.e - string.replace(/[YOUR_IGNORED_CHARACTERS]/g, "")
you can read more about regex here
Use this regex:
const regex = /\('([^']+)',\)/g
const output = input.replace(regex, '$1');
// explain: / \( ' ( [^']+ ) ',\) / g
// 1 2 3 4 5 6 7 8 9
- 1: start regex
- 2: match character
(
(character/
to escape) - 3: match character
'
- 4: start group in regex
- 5: match character not
'
- 6: close group in regex
- 7: match
',)
- 8: end regex
- 9: make this regex match global (don't need if you want replace one time)
$1
in code is first group match regex (start with 4 and end with 6)
const input = `('FCA',)
('JP NIC',)
('JP CHI',)
('2022-03-04T07:18:36.468Z',)`;
const output = input.replace(/\('([^']+)',\)/g, '$1');
console.log(input)
console.log('// =>')
console.log(output)
You could use a regex replacement with a capture group:
var inputs = ["('FCA',)", "('JP NIC',)", "('JP CHI',)", "('2022-03-04T07:18:36.468Z',)"];
var outputs = inputs.map(x => x.replace(/\('(.*?)',\)/g, "$1"));
console.log(outputs);
I think you are looking for String.Replace.
Replace: You can specify characters or even strings to be replaced in string.
How to remove text from a string? https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
Maybe i would create a custom method that handles the replacing and call that method with the proper parameters.
本文标签: javascripthow to remove Characters from a string in reactjsStack Overflow
版权声明:本文标题:javascript - how to remove Characters from a string in react.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744323996a2600640.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论