split a word if the word has capitalised words in between using JSTL - jstl

I want to split a word using JSTL
PaperTag: Paper, Tag
and i want to store it as:
word[0]: Paper;
word[1]: Tag;
I couldn't find any specific way to do this on google.

You can split string with JSTL
<c:set var="word" value="${fn:split('Paper, Tag', ',')}" />
For more: SPLIT WITH JSTL

Related

In JSP, how to escape a string value for JavaScript first, then for HTML? [duplicate]

This question already has an answer here:
How to escape JavaScript in JSP?
(1 answer)
Closed 6 years ago.
In a project using JSP and Spring, what would be the simplest way to escape an EL expression value for JavaScript first, then for HTML?
Imagine this use:
<a onclick='alert("${note}");'>Foo</a>
This is susceptible to XSS or plain syntax errors, as the variable value can contain quote, less-than and other characters.
What I came up is:
<a onclick='alert("<c:out value="${null}"
><s:escapeBody htmlEscape="false" javaScriptEscape="true"
>${variable}</s:escapeBody></c:out>");'>Foo</a>
<!-- this escapes first the inner tags body for JS,
then c:out uses that (because its value attribute
is null, it uses what is in its own body) and
escapes it for HTML/XML -->
It is rather clumsy, so I'm looking for a more elegant way.
(note that using just <s:escapeBody htmlEscape="true" javaScriptEscape="true"> is incorrect as the tag escapes first for HTML and then for JS, so for example it would fail on the value of a"b)
To avoid getting clumsy code, I would suggest you create a small custom taglib which provides a function exactly for that. You can implement that very easily by using StringEscapeUtils in commons-lang.
Then you can write:
<a onclick='alert("${foo:escapeJsHtml(note)}");'>Foo</a>

Listing tags using pandoc and YAML header

I'm writing in Markdown (with a YAML header) and trying to convert to HTML using pandoc. My YAML header has a "tags" field which is a list, like in the following example:
---
title: Title
tags: [one, two]
...
Text.
In the final result, I would like to display the tags nicely, in e.g. a comma-delimited list. However, in my pandoc template, the variable $tags$ only contains the first tag. Doing something like $for(tags)$$tags$, $endfor$ will generate all the tags, but the trailing comma and space are a problem. How can I display the tags list in the final HTML as one, two instead of just one or one, two,?
I had missed this in the official documentation under Templates:
When variables can have multiple values (for example, author in a
multi-author document), you can use the $for$ keyword:
$for(author)$
<meta name="author" content="$author$" />
$endfor$
You can optionally specify a separator to be used between consecutive
items:
$for(author)$$author$$sep$, $endfor$
So in my case I would do $for(tags)$$tags$$sep$, $endfor$ in the template.

Regexp to convert tags (similar to BBCode) to HTML

I have set of strings with nested [quote] tags in following format:
[quote name="John"]Some text. [quote name="Piter"]Inner quote.[/quote][/quote]
As you see it is not like ordinary BBCode. So I can't find a suitable regexp for gsub in Ruby to convert them to strings like this:
<blockquote>
<p>Some text.
<blockquote>
<p>Inner quote.</p>
<small>Piter</small>
</blockquote>
</p>
<small>John</small>
</blockquote>
Can anybody please help me with such regexp?
I'm pretty sure that regexes fundamentally can't cope with nesting. What you could do is make it do a minimal match (e.g. only the inner quote levels), replace them, and then repeat as long as you have more matches. Once you've replaced a level it will just be HTML so will not match the regex any more.

Why is my Ruby lookahead regex not working [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
RegEx match open tags except XHTML self-contained tags
I tested my regex in rubular.com and it works, but when I run the code it behaves differently.
I want to parse whole paragraphs out of some HTML code
Here is my regex
description = ad_page.body.scan(/(?<=<span id="preview-local-desc">).+(?=<\/span>)/m)
Here is some of the HTML source
<span id="preview-local-desc"> I want to pick up everything typed here.
Paragraphs, everything.
</span>
The match begins where I need it to but then it keeps matching all the way to the end of the document.
Aside from the fact that you shouldn't parse HTML with regex, you want non-greedy matching:
/(?<=<span id="preview-local-desc">).+?(?=<\/span>)/m
Parsing XML or HTML with a regex is marginally OK for trivial tasks, if you own or control the file's format. If you don't, then a simple change to the file could break your regex.
Using a parser will avoid that problem; I've parsed some horrible XML with Nokogiri and it didn't even notice. After writing a RSS aggregator that was handling 1000+ feeds I was hooked on using a parser.
require 'nokogiri'
html = '<span id="preview-local-desc"> I want to pick up everything typed here.
Paragraphs, everything.
</span>'
doc = Nokogiri.HTML(html)
doc.at('span').text
# => " I want to pick up everything typed here.\n Paragraphs, everything.\n "
If there are multiple <span> tags you want:
doc.search('span').map(&:text)
# => [" I want to pick up everything typed here.\n Paragraphs, everything.\n "]
If there are multiple <span> tags and you only want this one:
doc.at('span#preview-local-desc').text
# => " I want to pick up everything typed here.\n Paragraphs, everything.\n "

Ruby list of tags to a fluent regex

I want to clean an HTML page of its tags, using Ruby.
I have the raw HTML, and would like to define a list of tags, e.g. ['span', 'li', 'div'],
and create an array of regular expressions that I could run sequentially, so that I have
clean_text = raw.gsub(first_regex,' ').gsub(second_regex,' ')...
with two regular expressions per tag (start and end).
Do I have a way to do this programmatically (i.e. pre-build the regex array from a tag array and then run them in a fluent pattern)?
EDIT: I realize I actually asked two questions at once - The first about transforming a list of tags to a list of regular expressions, and the second about calling a list of regular expressions as a fluent. Thanks for answering both questions. I will try to make my next questions single-themed.
This should produce a single regexp to remove all your tags.
clean_text = raw.gsub(/<\/?(#{tags.join("|")})>/, '')
However, you have to improve it to support tags with attributes (e.g. <a href="...">), currently only simple tags are removed (e.g. <a>)
Assuming you have a build_regex method to turn a tag into a regex, this should do it:
tags = %w(span div li)
clean_text = tags.inject(raw) {|text, tag| text.gsub build_regex(tag), ' ' }
The inject call passes the result of each substitution into the next iteration of the block, giving the effect of running each gsub on the string one by one.

Resources