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 6 years ago.
Improve this question
I am having JavaScript string combinations like this,
str = "<div>xyz 123 <rd> </div>"
or
str = "sample text <abc>"
or
str = "Text <input type="text" /> new value <bla> ."
I have to find invalid tags(not having end tags) like (<rd>, <abc>, <bla>) and need to replace like <bla>
Can someone help to solve this. Fully in JavaScript.
Using jQuery you can do something like this with help of replace()
var str = `<div>xyz 123 <rd> </div>
sample text <abc>
Text <input type="text"/> new value <bla>`;
console.log(
str.replace(/<([a-z]+)([^>]*[^\/])?>(?![\s\S]*<\/\1)/gi, function(m) {
return $('<div/>').text(m).html(); // encoding matched text
})
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Regex explanation here
Refer the following answer for html encoding : HTML-encoding lost when attribute read from input field
Related
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);
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
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);
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, ''))
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 7 years ago.
Improve this question
I am trying to be able to use replace(searchvalue, newvalue); and a user input that would be the search term.
An example would be replace(input,"example text");
But what I want is to be able to have the search term, but instead of replacing the search term, replace the space in front of it.
Ex. is the sentence: "Hi, I am using js to create this!"
and user inputs "js" replace(input, "html and ");
but instead of replacing "js", replace the space in front. So the output sentence would be:
"Hi, I am using html and js to create this!"
Would there be anyway to do this with replace?
You can use a function to handle the replacement in string.replace.
var newString = 'Hi, I am using js to create this!'.replace('js', function(match) {
return 'html and ' + match;
});
console.log(newString); // ... using html and js ...
In the above example I prepend "html and " to the variable match (which has the value of "js"). There is a lot of flexibility when you use a regex instead of a string to find the match.
MDN