This question already has answers here:
Unescape HTML entities in JavaScript?
(33 answers)
Closed 19 days ago.
is there a special function for this replacement in Javascript ?
(replaceAll)
PARAMS+= "&Ueberschrift=" + ueberschrift.replaceAll(">",">").replaceAll("<","<");
PARAMS+= "&TextBaustein=" + textBaustein.replaceAll(">",">").replaceAll("<","<");
PARAMS+= "&Beurteilung=" + beurteilung.replaceAll(">",">").replaceAll("<","<");
edit: there is a replaceAll() method in JS, my bad !
anyhow, you can use the replace() method and use a regular expression to replace all occurrences of a string, taken from your provided example you could do something like this:
PARAMS += "&Ueberschrift=" + ueberschrift.replace(/\>/g, ">").replace(/\</g, "<"); PARAMS += "&TextBaustein=" + textBaustein.replace(/\>/g, ">").replace(/\</g, "<"); PARAMS += "&Beurteilung=" + beurteilung.replace(/\>/g, ">").replace(/\</g, "<");
To elaborate:
'g' flag indicates that the replacement should occur for all matches (not just the first one)
> and < characters are escaped to match the actual > and < characters.
'>' and '<' (HTML escape codes for '>' and '<')
Related
This question already has answers here:
Replace all occurrences of character except in the beginning of string (Regex)
(3 answers)
Remove all occurrences of a character except the first one in string with javascript
(1 answer)
Closed 1 year ago.
I’m looking to remove + from a string except at the initial position using javascript replace and regex.
Input: +123+45+
Expected result: +12345
Please help
const string = '+123+45+'
console.log(string.replace(/([^^])\+/g, '$1'))
https://regexr.com/61g3c
You can try the below regex java script. Replace all plus sign except first occurrence.
const givenString = '+123+45+'
let index = 0
let result = givenString.replace(/\+/g, (item) => (!index++ ? item : ""));
console.log(result)
input = '+123+45+';
regex = new RegExp('(?!^)(\\+)', 'g');
output = input.replace(regex, '');
console.log(output);
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 2 years ago.
I have the following string:
"Write the conjugate of each radical expression.\n\n**(a)** `$2\\sqrt{3} - 4$`\n\n**(b)** `$\\sqrt{3} +\\sqrt{2}$`\n\n**(c)** `$-2\\sqrt{3} - \\sqrt{2}$`\n\n**(d)** `$3\\sqrt{3} + \\sqrt{2} $`\n\n**(e)** `$\\sqrt{2} - \\sqrt{5}$`\n\n**(f)** `$-\\sqrt{5} + 2\\sqrt{2}$`"
And I have the following function to go through a string and replace substrings:
var changeString = function(markdownStr) {
return markdownStr.replace(/`/g, "").replace("$ ", "$").replace(" $", "$");
};
The result I get is that it replaces some of the conditions (the `), but it didn't work for the last replace condition (" $").
Here is the output:
Write the conjugate of each radical expression. **(a)**$2\sqrt{3} - 4$ **(b)** $\sqrt{3} +\sqrt{2}$ **(c)** $-2\sqrt{3} - \sqrt{2}$ **(d)** $3\sqrt{3} + \sqrt{2} $ **(e)** $\sqrt{2} - \sqrt{5}$ **(f)** $-\sqrt{5} + 2\sqrt{2}$
You can see for the (d) option, it still outputs as $3\sqrt{3} + \sqrt{2} $ but I expected it to be $3\sqrt{3} + \sqrt{2}$.
What is going on and why isn't it replacing it?
Here is a codepen example:
https://codepen.io/jae_kingsley/pen/MWyWZbN
From W3Schools
If you are replacing a value (and not a regular expression), only the
first instance of the value will be replaced
So you should probably use regular expressions for all replaces, not just the first one. Don't forget you'll have to escape the $:
.replace(/\s\$/g, '$')
Changing you code to following will work as it accounts for any $ with space before or after or none. So it will match $, $ , $, $ etc.
var changeString = function(markdownStr) {
return markdownStr.replace(/`/g, "").replace(/\s*\$\s*/g, '$');
};
This question already has answers here:
how to extract floating numbers from strings in javascript
(3 answers)
Closed 4 years ago.
I have string like
11.7 km
and I need get only int( 11.7 ), how can I do this with JavaScript?
Thanks.
Try the parseInt().
Example:
var string = "11.7 km";
alert(parseInt(string));
This would alert: "11".
In your case you have a float, so you could use:
alert(parseFloat(string));
This gives an alert with "11.7".
ParseFloat reference
ParseInt reference
You can use replace method by passing a regex expression as argument.
console.log('11.7 km'.replace(/[^0-9.]/g, ''));
Try This.
var numbers = distance.replace(/[^0-9.]/g,'');
alert(numbers);
You can use parseFloat('11.7km') this will return 11.7
You can also use this.
console.log(Number(("11.7 km").replace(/[^\d.-]/g, '')));
This regular expression will match all numerical values in a string, regardless of text before or after the value.
var str = "11.7 km\n" +
"Text before 11.7 km\n" +
"11.7 km Text after\n" +
"Text before 11.7 km Text after\n";
var matches = str.match(/(\.\d*)*\d+(\.\d*)*/igm).map(parseFloat);
console.log('string "' + str + '" has ' + matches.length + " matches:");
console.log(matches);
The solution you can use is a Regular Expression, using the match function:
your_text = "11.7 km";
r = new RegExp("\\d|\\.","g");
matched_number = your_text.match(r);
number = matched_number.join("");
number_float = parseFloat(number)
This question already has answers here:
Use dynamic (variable) string as regex pattern in JavaScript
(8 answers)
Closed 4 years ago.
As you can see below, I'm trying to count how many times a character in string J occurs in string S. The only issue is I can't put the argument o in the forEach loop into the regex expression as shown in the console.log.
var numJewelsInStones = function(J, S) {
let jArr = J.split('');
let sArr = S.split('');
jArr.forEach(o=>{
console.log(S.replace(/[^o]/g,"").length);
})
};
numJewelsInStones("aA", "aAAbbbb");
You can create regular expression with constructor function where you pass string parameters:
new RegExp('[^' + o + ']', 'g')
Your replace logic might look like:
S.replace(new RegExp('[^' + o + ']', 'g'), '')
This question already has an answer here:
Javascript regex match fails on actual page, but regex tests work just fine
(1 answer)
Closed 4 years ago.
I am trying to replace the nth occurrence of a character with the following function
It works for strings of letters but I want to replace the 2nd [ in [WORLD!] HELLO, [WORLD!]
I am trying the pattern /.\\[/ which works in RerEx tester but not in my function. I get no error just no replacement
Thanks
function ReplaceNth_n() {
Logger.log(ReplaceNth("[WORLD!] HELLO, [WORLD!]", "/.\\[/", "M", 2))
}
function ReplaceNth(strSearch,search_for, replace_with, Ocur) {
var nth = 0;
strSearch = strSearch.replace(new RegExp(search_for, 'g'), function (match, i, original) {
nth++;
return (nth === Ocur) ? replace_with : match;
});
return strSearch
}
When you create a regular expression with RegExp, you should not include the opening and closing / in the string, and I also don't understand why you added a . there.
Wrong: new RegExp("/test/");
Correct: new RegExp("test");
So the string passed as parameter for search_for should be \\[.
The nth occurrence.
^([^<char>]*(?:<char>[^<char>]*){<N-1>})<char>
Replace with $1<repl_char>
Where the regex string is constructed
regexStr = '^([^' + char + ']*(?:' + char + '[^' + char + ']*){' + (N-1) + '})' + char;
Where char is to be found and N > 0