hiere is my code, within a loop.
I want to solve my closure problem.
But on click I receive an error message "Syntac Error".
Any help. Thanks in advance.
var html = '<div style="margin-top:10px">';
html += '<div class="weiter"> Kartenauswahl in die Recherche übernehmen </div>';
html += '<div class="weiter" style="display:block;clear:left;margin-top:7px"
onclick="function(obj) { return getChildObject(obj)}(obj)">Weiter</div>';
html += '</div>';
This certainly does not look right:
function(obj) { return getChildObject(obj)}(obj)
I would simply write this:
return getChildObject(obj);
This code in your onclick attribute is not valid:
function(obj) { return getChildObject(obj)}(obj)
Put the function expression in parentheses and try this instead:
(function(obj) { return getChildObject(obj); })(obj)
If that's your actual code, the immediate problem is that there's a line break in your string literal, between the third and fourth lines. Delete that line break. There are problems in the HTML too, as mentioned in other answers.
The problem seems to be at:
onclick="function(obj) { return getChildObject(obj)}(obj)
^
Try removing (obj).
Also you should post the code for getChildObject function.
function(obj) { return getChildObject(obj)}(obj)
Above does not seems to be correct
Related
I am currently working on a project where I need to match specific html tags and replace them by some others.
I am using Javascript in order to do so, and the code looks like :
// html to update
html = '<div class="page-embed"><article><iframe src="https://alink" width="100%"></iframe></article></div>';
// regex that will match the specific "<div class="page-embed">...<iframe src="https://alink"></iframe>...</div>
const regexIframeInsideDiv = /<\s*div\s*class="page-embed"[^>]*>.*?<\s*iframe[^>]*\s*src="(.*?)"\s*><\s*\/\s*iframe\s*>.*?<\s*\/\s*div\s*>/g;
html = html.replace(regexIframeInsideDiv, (_match, src) => {
console.log(src);
return `<oembed>${src}</oembed>`;
});
I use the () tool to get what is inside the source attribute as follow :
src="(.*?)"
Here is the problem :
If I run the code, the console will log :
https://alink" width="100%
where it should log :
https://alink
I might be missing something, like escape string or an error anything else.. but I don't know what.
Here is the expected behaviour :https://regexr.com/4tbj6
Thank you !
In your regex, on the part you are matching src, it's not \s* but \s.*
src="(.*?)"\s.*>
// html to update
html = '<div class="page-embed"><article><iframe src="https://alink" width="100%"></iframe></article></div>';
// regex that will match the specific "<div class="page-embed">...<iframe src="https://alink"></iframe>...</div>
const regexIframeInsideDiv = /<\s*div\s*class="page-embed"[^>]*>.*?<\s*iframe[^>]*\s*src="(.*?)"\s.*><\s*\/\s*iframe\s*>.*?<\s*\/\s*div\s*>/g;
html = html.replace(regexIframeInsideDiv, (_match, src) => {
console.log(src);
return `<oembed>${src}</oembed>`;
});
Try this RegEx:
(?<=(<div class="page-embed".+iframe src="))(.*?)(?=")
Which searches for a String between src=" and the next " in a div with your class and an iframe.
Another stupid question.
I believe I understand this right but it doesn't seem to work.
function parseCustomCommands($text, $textParts) {
if($this->getUserRole() == AJAX_CHAT_ADMIN || $this->getUserRole() == AJAX_CHAT_MODERATOR) {
switch($textParts[0]) {
case '/takeover':
$this->insertChatBotMessage( $this->getChannel(), $text );
return true;
default:
return false;
}
}
}
ajaxChat.replaceCustomCommands = function(text, textParts) {
switch(textParts[0]) {
case '/takeover':
text=text.replace('/takeover', ' ');
return '<span class="chatBotMessage">' + text + '</span>';
default:
return text;
}
}
It's executed when /takeover is this sent and the way I'm looking at it the '/takeover' part is meant to be replaced with nothing leaving just the . This does not seem to be the case..
Is anyone able to point out the mistake in it? I've tried several things with $ in variable names and using different variables to remove it.
$re="/takeover";
$str=text.replace(re, ' ');
I've tried too.
Thanks in advance.
This is really a PHP question
github source
Review the other "insertParsedMessage*" PHP methods
I'm not a PHP developer but it should be something like this based on the documentation
$text = str_replace("/takeover","", $text);
trying to parse some content (no DOM available - or DOM parser for that matter i.e. jQuery, Cheerio) to replace some words/symbols (basically emotions) by images, BUT would like to ignore everything in between <code></code> and <pre></pre> this example works great on replacing all the emotions, but doesn't ignore code and pre tags
http://jsbin.com/odARehI/5/edit?js,console
if you run the script, you will see the first print out before the code tag and the second after.
would appreciate another set of eyes on that pattern. Thanks
// see link for a list of the emotions to parse
var pattern = />:\)|\([\w~]+\)|\\[:]?[od]\/|[:;\|bBiIxX8\(\)\]][=\-"^:]?[)>$&|\w\(\)*##?]?[)>$&|\w\(\)*##?]/g;
I tried few things that didn't work without messing up the original match.
For the Don't-parse-html-with-regex-police-department: this is running server side and I do not have the luxury for a DOM parser at the moment.
Thank you.
UPDATE: for a RegExp solution to ignore <code> tags see this neat solution thanks to github/frissdiegurke in this commit
/(^|<\/code>)([^<]*|<(?!code>))*(<code>|$)/g
Without DOM parsing you are going to have edge cases which will fail. But, this should work for you.
Given this HTML:
Hello :) <pre>Wassup :)</pre> Maybe :) <code>:) Foo</code> :) Bar
Use this code:
var blocks = [];
html = html.replace(/(?:<pre>.*?<\/pre>|<code>.*?<\/code>)/g, function (match) {
blocks.push( match );
return '__BLOCK__';
});
html = html.replace(/:\)/g, 'SMILE');
html = html.replace(/__BLOCK__/g, function () {
return blocks.shift();
});
Which produces:
Hello SMILE <pre>Wassup :)</pre> Maybe SMILE <code>:) Foo</code> SMILE Bar
Just adjust the /:\)/g replace to work however you need it.
Guess you're using nodejs or a recent javascript engine (for "map" & "split" implementations), so you can do this:
function replaceSpecial(str, pattern, replacement) {
var REG = /(<code>.*?<\/code>)|(<pre>.*?<\/pre>)/i;
return str.split(REG).map(function(s) {
if ('' + s !== s)
return '';
if (s.match(REG))
return s;
return s.replace(pattern, replacement);
}).join('');
}
Example:
replaceSpecial("hey :) <code>:)</code> :'( <pre> :'( :)</pre>", /(:\))|(:'\()/, function(s) {
switch(s) {
case ":)":
return '<img src="smile.gif" />';
case ":'(":
return '<img src="cry.gif" />';
}
})
Will return:
"hey <img src="smile.gif" /> <code>:)</code> <img src="cry.gif" /> <pre> :'( :)</pre>"
Or easier if you just want to replace an emoticon:
replaceSpecial("hey :) <code>:)</code>", ":)", '<img src="smile.gif" />')
=>
"hey <img src="smile.gif" /> <code>:)</code>"
var co = -1, ce = 0, start=0, result;
while ( ce != -1 ) {
co = testString.indexOf('<code', ce);
if (co > -1) {
result += parse(testString.substring(start,co), pattern1);
start = co+1;
ce = testString.indexOf('</code>', co + 5);
if (ce >-1 ){
start = ce + 7;
ce = start;
result += testString.substring(co,ce);
}
}
}
result += parse(testString.substring(start), pattern1);
console.log(result);
The links work in this example, but the onClick does nothing. When I display the productURL[i] in an alert() it shows the correct URL. Any suggestions?
var output='<table class="api-table">';
output+='<thead><tr><th colspan="2">' + productSubstrateName + '</th></tr></thead>';
for (var i=0;i<productURL.length;i++) {
output+='<tr>';
output+='<td style=\"cursor:pointer;\" onClick=\"'+productURL[i]+'\">'+productSubstrateAmounts[i]+'</td>';
output+='<td style=\"cursor:pointer;\" onClick=\"'+productURL[i]+'\">'+productSubstratePrices[i]+'</td>';
output+='</tr>';
}
output+="</table>";
$('#'+outputdiv).append(output);
but the onClick does nothing.
it does nothing becuase you have done nothing there ..you are just printing the value ..
onClick=\"'+productURL[i]+'\"
//--^^^^^^^^^^^----
if you need to do something then you can call a function there
onClick="myFunction("'+productURL[i]+'")"
and your function
function myFunction(obj){
alert(obj);
}
and you don't have to use \ there
Beginners mistake, thanks bipen, I forgot the document.location.href. :-(
for (var i=0;i<productURL.length;i++) {
output+='<tr>';
output+='<td style="cursor:pointer;" onClick="document.location.href=\''+productURL[i]+'\';">'+productSubstrateAmounts[i]+'</td>';
output+='<td style="cursor:pointer;" onClick="document.location.href=\''+productURL[i]+'\';">'+productSubstratePrices[i]+'</td>';
output+='</tr>';
}
I have a string:
<Grid><Description>LINE 1
LINE 2
LINE 3
LINE 4
</Description><</Grid>
I need it to be decoded with line breaks. I found solution:
function decodeString(stringToDecode) {
if (!stringToDecode) {
return stringToDecode;
}
return $('<div />').html(stringToDecode).text();
}
But it makes single line and replaces all the line breaks with spaces.
you may use the following to replace your line breaks with <br /> tags an then set the HTML:
return $('<div />').html(stringToDecode.replace(/\n/, '<br />')).text();
function decodeString(stringToDecode) {
return stringToDecode ? $('<div />').html(stringToDecode.replace(/[\n\r]/g, "<br> \r\n")).text() : ""
}
Your sample has CR/LF - but that is not a line break in HTML. You need to replace it with a valid HTML line break ie the < br > tag.
Your function has a strange if statement that does not make much sense. Why return stringToDecode when you've just proven it is null? Also, $('<div />').html(stringToDecode).text() will not do anything helpful.
Try something like this:
function decodeString(stringToDecode) {
if (!stringToDecode) {
return "";
}
var regX = /\\n/g;
var replaceString = '<br> \\n';
return stringToDecode.replace(regX, replaceString);
}