This question already has answers here:
How to use JavaScript regex over multiple lines?
(8 answers)
Closed 5 years ago.
I am trying to replace the string {NUM} in the following variable:
var table = ' <table class="full-width">\r\n' +
' <tbody>\r\n' +
' <tr>\r\n' +
' <td width="75%" class="border-right-dotted left-line-tab">\r\n' +
' <span id="47_TOTAL-CHARGES_D_{NUM}" class="input-text"></span>\r\n' +
' </td>\r\n' +
' <td width="25%" class="center-text">\r\n' +
' <span id="47_TOTAL-CHARGES_C_{NUM}" class="input-text"></span>\r\n' +
' </td>\r\n' +
' </tr>\r\n' +
' </tbody>\r\n' +
' </table>\r\n';
using the following jquery replace:
table = table.replace("/{NUM}/gm", num);
but it doesn't seem to work. Testing the regex in https://regex101.com/ seems to present that the regex is fine, but it still does not replace the text as expected.
Just remove the double quotes:
table = table.replace(/{NUM}/gm, num);
A text within double quotes defines a string literal whilst you need a regular expression literal, which should be surrounded by slashes.
Related
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 '<')
This question already has answers here:
Are double and single quotes interchangeable in JavaScript?
(23 answers)
Closed 6 months ago.
What is the correct way to pass the single quotes inside the [ ] in this url please in my Apps Script?
https://{{domain}}/api/1.0.0/{apiKey}/appointments?where=['start,>=,2017-05-26T07:23:46-07:00','end,<=,2017-05-26T07:23:46-07:00']
I have:
var url = 'https://{{domain}}/api/1.0.0/' + {apiKey} + '/appointments?where=[' + ? + 'start,>=,2017-05-26T07:23:46-07:00' + ? +',' + ? + 'end,<=,2017-05-26T07:23:46-07:00' + ? + ']'
Why not simply use double quote ".
var url = "https://{{domain}}/api/1.0.0/" + {apiKey} + "/appointments?where=['start,>=,2017-05-26T07:23:46-07:00','end,<=,2017-05-26T07:23:46-07:00']";
I am trying to make a unicode code points table that prints the code points till U+300
I change the number into hexadecimal and concatenate it with the unicode escape sequence.
When I try to concatenate the hexadecimal number with '\u' I get an error SyntaxError: Invalid Unicode Escape Sequence
Here's the code
How can I fix that error?
Change the print statement to print(num + ' => ' + String.fromCharCode("0x" + num));
Instead of this:
print(num + ' => ' + '\u' + num);
use this:
print(num + ' => ' + '\\u' + num);
Or, more concisely,
print(num + ' => \\u' + num);
You need to escape the \ itself to include it in a string literal.
I have following regular expression to check only one decimal point for type number tag in html
^-?[0-9]*\\.?[0-9]*$
but this regular failed to check If I put decimal at the end e.g 12.12.
what further I have to add to check this
I think your regex can be easily fixed using a + instead of last * quantifier:
^-?[0-9]*\.?[0-9]+$
Tests:
const regex = /^-?[0-9]*\.?[0-9]+$/gm;
console.log('regex.test?')
console.log('12 = ' + regex.test('12'));
console.log('12. = ' + regex.test('12.'));
console.log('12.1 = ' + regex.test('12.1'));
console.log('12.12. = ' + regex.test('12.12.'));
console.log('-1 = ' + regex.test('-1'));
console.log('-1. = ' + regex.test('-1.'));
console.log('-1.2 = ' + regex.test('-1.2'));
console.log('-.12 = ' + regex.test('-.12'));
console.log('-. = ' + regex.test('-.'));
console.log('-. = ' + regex.test('-'));
console.log('. = ' + regex.test('.'));
Demo
Can you try the below : [1-9]\d*(\.\d+)?$
The simplest way to allow a possible . at the end is to have \.? just before the $. Also, the double \ looks wrong (unless you need it for escaping a \ in the context in which you are using it):
^-?[0-9]*\.?[0-9]*\.?$
But please recognize that your regex does not require any actual digits, so will match some non-numbers, like ., -. and (with my edit) -.. The above regex will also match an empty string!
You will want to either change your regex to require digits, or take into account somewhere else that they might not be there.
This question already has answers here:
JS replace not working on string [duplicate]
(2 answers)
Closed 8 years ago.
I'm trying to replace a placeholder in a string I'm generating.
My string looks like this:
var s = 'module("SlapOS UI Basic Interaction"); ' +
'asyncTest( "${base_url}", function() { ' +
' expect( __number__ ); ' +
' ok(testForElement("div#global-panel"), "element present");' +
' start(); })';
And I want to replace __number__.
I can get the index correctly like so:
s.indexOf("__number__");
but replacing does not work...
s.replace("__number__", "1");
Question:
What am I doing wrong here? Makes no sense to my why it does not work.
The replace method does not modify the existing string. It returns a new one.
var result = s.replace("__number__", "1");