Remove space between words in string javascript js React [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm using React (hooks) and in a component I have postcodes with a space in them in strings:
eg "B72 1JL".
I need to remove the space in the middle of the postcodes so it is "B721JL" and also possibly any spaces before or after the postcode.
I've googled for ages and cannot find anything that will work. I know i probably need regex...but pretty confused!
Help appreciated.

Use String.prototype.replace() to change the space for an empty space.
const str = "B72 1JL";
// Replacing " " (space) to "" empty space
const res = str.replace(/ /g, '')
console.log(res); // BJ721JL

Related

Replace string pattern but keep part of it [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
I have a long document with some headings I want to replace in a operation.
The headings have the following structure (there are two nouns, both in with a uppercase first character, separated with a whitespace, also the time is dynamic):
let string = 'Firstname Lastname [00:01:02]';
I want to insert some characters at the front and the end of this string, but want to keep the content.
So the desired output should be something like:
let string = '{Firstname Lastname [00:01:02]}:';
I tried a little bit around with RegEx and can catch the time with the following pattern:
\[[0-9]{2}:[0-9]{2}:[0-9]{2}
I figured it out by using captures in my RegEx.
/(\b[A-Z][a-z]* [A-Z][a-z]*( [A-Z])?\b\s\[[0-9]{2}:[0-9]{2}:[0-9]{2}\])/g
This RegEx captures the pattern of my headings into group one. In a replace operation I can then insert the desired content.
string.replace(regex, '{$1}:')

Javascript remove all ',' in a string with regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
i want remove all ',' from my string with regex in javascript.
this is an example from my string:
45,454,545
and i want my string convert to this:
45454545
Comma isn't a special character in regex, so you can just use /,/. Add the global flag and you're done.
console.log('45,454,545'.replace(/,/g, ''))
Try this,
var str = "45,454,545";
var res = str.replace(/,/g, "");
console.log(res);

How to remove a group of characters from a phone string in javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am willing to know how can I check if the a phone number contain the following prefixes +44, 0044 or 0 and if so it will be removed? I know I to remove a number of characters or a substring but how do I check if that substring is in the beginning?
Thanks in advance
You could with regex /^(\+44|0044|0)/g
function rem(str){
return str.replace(/^(\+44|0044|0)/g,'')
}
console.log(rem('0044987987'));
console.log(rem('04478687'));
console.log(rem('+447783'));
You can use indexOf to check the index of those prefix.
"+44-----".indexOf("+44") // returns 0
That should be enough for what you want.
You can simply test against a regex pattern.
Or use it for a replace.
const re_tel44 = /^[+]?0{0,2}(?:44)?(\d{5,})$/;
let phonenumber = '+4412345';
console.log(re_tel44.test(phonenumber));
if(re_tel44.test(phonenumber))
phonenumber = phonenumber.replace(re_tel44, '$1');
console.log(phonenumber);

How to retrieve substring which is outside of brackets in JS? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want the text which is outside of brackets, for eg.
Text is - Outside (inside)
and what I expect is - Outside
Can someone please help me to achieve this.
You can use slice & use indexOf to get the first (. This will extract all the characters before first (
let str = 'Outside (inside)'
let substr = str.slice(0, str.indexOf('('));
console.log(substr.trim())
If you wanted to remove all bracketed text from the string you could use
let str = 'Outside (inside)test(d 342 dd3d)dd(t423t t)dd()fasf(fsdfds32dfs)';
console.log(str.replace(/(\([\w\d ]*\))+/g, ''))

How to split string with several char [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to split string with several char
i have to split a scope when there are spaces, comma, dash, etc..( ponctuation) and when words are concatenated ( without a space between variable)
For exemple
testOne="{{test.test}} {{test.test}}{{test.test}}";
The expected output is
"test.test test.testtest.test"
(There are two comma between the first and the second text.text)
You can use Regular Expression to get the result what you wanted.
testOne = "{{test.test}} {{test.test}}{{test.test}}";
console.log(testOne.match(/{{.*?}}/g).map(function(item) {
return item.replace(/[{}]/g, "");
}));
# [ 'test.test', 'test.test', 'test.test' ]
You can achieve your desired output with a simple replace
testOne.replace(/{{(.*?)}}/g, '$1');
// "test.test test.testtest.test"
Remember to set this back to testOne if you want it kept as that variable

Categories