Multi lines within ul element breaking my regex pattern [duplicate] - regex

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 3 years ago.
Live example:
https://regexr.com/4rblr
Source markup:
<xf:macro name="breadcrumbs" arg-breadcrumbs="!" arg-navTree="!" arg-selectedNavEntry="{{ null }}" arg-variant="">
<xf:if contentcheck="true">
<ul class="p-breadcrumbs {{ $variant ? 'p-breadcrumbs--' . $variant : '' }}"
itemscope itemtype="https://schema.org/BreadcrumbList">
<xf:contentcheck>
<xf:set var="$position" value="{{ 0 }}" />
<xf:set var="$rootBreadcrumb" value="{$navTree.{$xf.options.rootBreadcrumb}}" />
<xf:if is="$rootBreadcrumb AND $rootBreadcrumb.href != $xf.uri">
<xf:set var="$position" value="{{ $position + 1 }}" />
<xf:macro name="crumb"
arg-position="{$position}"
arg-href="{$rootBreadcrumb.href}"
arg-value="{$rootBreadcrumb.title}" />
</xf:if>
<xf:if is="$selectedNavEntry && $selectedNavEntry.href && $selectedNavEntry.href != $xf.uri && $selectedNavEntry.href != $rootBreadcrumb.href">
<xf:set var="$position" value="{{ $position + 1 }}" />
<xf:macro name="crumb"
arg-position="{$position}"
arg-href="{$selectedNavEntry.href}"
arg-value="{$selectedNavEntry.title}" />
</xf:if>
<xf:foreach loop="$breadcrumbs" value="$breadcrumb" if="$breadcrumb.href != $xf.uri">
<xf:set var="$position" value="{{ $position + 1 }}" />
<xf:macro name="crumb"
arg-position="{$position}"
arg-href="{$breadcrumb.href}"
arg-value="{$breadcrumb.value}" />
</xf:foreach>
</xf:contentcheck>
</ul>
</xf:if>
</xf:macro>
I'm trying to grab the ul.p-breadcrumbs element via regex. I can grab the ul and everything after ok using:
<p.*class.p-breadcrumbs(.*[\s]*)*
And I can select the closing tag with
<\/ul>
However putting them together doesn't work at all:
<ul.*class=.p-breadcrumbs(.*[\s]*)*<\/ul>
I followed this answer:
https://www.regextester.com/93456
But it doesn't work with multiline content. If you line break that example and enter more content it breaks the solution. Appreciate any pointers as I'm pulling hair out trying to solve the last bit!

I fixed it on my own as a regex novice by fiddling around. The first group needed a ? symbol and I don't know why, other than it somehow makes matches "lazy" and not grab too many characters.
Solution is updated with:
<ul class="p-breadcrumbs(.*?\s)*?<\/ul>
https://regexr.com/4rblr
Perhaps someone with more knowledge could demystify the ? usage for other novices?

Related

Struts 2 radio, disable specific option [duplicate]

This question already has an answer here:
How to set value to for a textarea, and get it with struts2 actions?
(1 answer)
Closed 4 years ago.
I want to disable a particular option in a radio button group with struts.
I am able to pre-select an option using below code, can I pre-disable an option too?
<s:radio label="Answer" name="yourAnswer" list="#{'1':'Yes','2':'No'}" value="2">
With iterator I got what I wanted -
<s:iterator value="genders" status="stat" var="gender">
<s:if test="%{#gender == 'male'}">
<input type="radio" name="gend" value="<s:property />" disabled="disabled">
<s:property/>
</s:if>
<s:else>
<input type="radio" name="gend" value="%{stat+'x'}">
<s:property/>
</s:else>
</s:iterator>

How to test if the input value contain string

In my html 5 page, I want to force the user to add in the end of his login the ".app" string.
Do you know how I can make that ?
My current code:
<input type="text" name="user_login" placeholder="firstname.app" required style="width:92%;" pattern="/.app/">
Thanks you for your help.
EDIT
I have resolved my problem with a jQuery check and a html 5 pattern
JS:
// If the user login doesn't finish by .app, we add them
if(!$('input[name=user_login]').val().match('[.]app$'))
$('input[name=user_login]').val($('input[name=user_login]').val() + '.app');
HTML5:
<input type="text" name="user_login"
placeholder="firstname.app" required style="width:92%;"
pattern=".*\.app">
There are a few differences between regex in JavaScript and in HTML attributes :
you don't have to put your regex between /, contrary to js regex literals
the start and end are implicit, so here you should add .* to match the start
You also forgot to escape the dot.
All in one, you probably want this :
<input type="text" name="user_login"
placeholder="firstname.app" required style="width:92%;"
pattern=".*\.app">
Demonstration
try this:
if($('input[name="user_login"]').val().indexOf(".app") >= 0){
//code
}
or pattern:
<input type="text" name="user_login" placeholder="firstname.app" required style="width:92%;" pattern="\w*.app">
Here is demo
You could do it by specifying the specific pattern:
<input type="text"
name="user_login"
placeholder="firstname.app"
required style="width:92%;"
pattern="[A-Za-z-0-9]+\.app">

Can I avoid this syntax highlighting confusion?

I have the following in my Django view:
<a title="{{ photo.time_taken|date:"N jS, 'y" }}" href='...' > {# ' #}
...
</a>
The problem is with wanting to format my date with the year as '13. The quotes get all confused, so I've had to put a django comment on the end to 'close' the quote, otherwise all following code is mis-highlighted. Is there a better way around this?
I'm using Sublime Text 2, with Djaneiro.

What is the best practice to create array notation fields in play-framework templates?

In one of my Play! projects I use a table of inputs to update multiple objects in one go.
I pass it to the controller as an array of objects. This works quite well, but the template looks quite a mess, since I'm forced to write the whole array index notation.
The template in its current state:
#{list items:_articles, as:'article' }
<tr>
<td>${article.number}</td>
<td>${article.name}</td>
<td>${article.totalPrice}</td>
<td><input type="text" name="${ 'articles[' + article_index + '].description'}" value="${article.description}" /></td>
<td>
#{select 'articles[' + article_index+ '].consignment', value:article.consignment}
#{option ''} -- #{/option}
#{option 'Foo'} Foo #{/option}
#{option 'Bazz'} Bazz #{/option}
#{option 'Bar'} Bar #{/option}
#{/select}
<input type="hidden" name="${ 'articles[' + article_index + '].id'}" value="${article.id}" />
</td>
</tr>
#{/list}
What - if there is one - is the best-practice to create array-notation fields using the play-framework?
Create a seperate variable for the 'articles[' + article_index + '] inside your loop?
Or; Maybe you just can leave it out? Create an array without your article index:
<input type="hidden" name="article.id[]" />
<input type="text" name="article.name[]" />
Later on, in your loop you can figure out the array index, and the corresponding id. Problem is you can't have optional fields this way. I won't recommend it.

How to validate characters ONLY in ColdFusion CFForm?

I've got a very simple cfform with a single form field:
<cfform action="asdf.cfm" method="post">
<cfinput name="fieldName" type="text" size="20" maxlength="20" required="yes" validate="regex" pattern="[A-Za-z]" message="Only characters are allowed." />
<input type="submit" name="btnSubmit" value="check" />
</cfform>
Theoretically, this would allow ONLY A-Z and a-z in any combination, and must have some content in it.
In practice, I'm able to enter 'a a' and the javascript validation does not complain. Since the 'space' character is not in A-Z and not in a-z, what's going on?
Thanks!
Chris
You are missing the start-of-string and end-of-string anchors:
^[A-Za-z]$
or, more likely:
^[A-Za-z]{1,20}$
Your sample, modified:
<cfform action="asdf.cfm" method="post">
<cfinput name="fieldName" type="text" size="20" maxlength="20" required="yes" validate="regex" pattern="^[A-Za-z]{1,20}$" message="Only characters are allowed." />
<input type="submit" name="btnSubmit" value="check" />
</cfform>
Without those anchors, the regex merely needs to match anywhere, it does not need to match entirely.
personally I would avoid using the built in coldfusion javascript. You will have much more control if you roll your own and it will give you the ability to display errors in other ways than an alert box.
<script>
function checkit() {
var v = document.getElementById("text1").value;
if(!v.match(/^[a-zA-Z]+$/)) {
alert(v + ' contains invalid characters');
return false;
}
return true;
}
</script>
<form onsubmit="return checkit()">
<input type="text" id="text1">
<input type="submit">
</form>
<script>
function checkit() {
var v = document.getElementById("text1").value;
if(!v.match(/^[a-zA-Z\\ \\.\\]+$/)) {
alert(v + ' contains invalid characters');
return false;
}
return true;
}
</script>
<form onsubmit="return checkit()">
<input type="text" id="text1">
<input type="submit">
</form>
Here is also enter and point possible, but how I'm must do to reconoce ä å ö.
Of course thanks for all help, You help so much.
Came across this article while I was searching for a solution of my own. Needed a non-intrusive way to keep people from entering anything by numbers in one field and nothing but letters, numbers or spaces in other fields. I couldn't use pattern="9999" for the numbers as it was not a required field and folks get "yelled at" if they tab through that field. Likewise, could not use pattern="xxx" for the alpha/numeric fields as I also needed to allow spaces.
Leapfrogging from this article and using javascript that previous programmers had developed for that client, I came up with these beautiful handlers, thought I'd share in case someone else needed this elegant solution and ALSO because sometimes I forget and would be able to find this again.
Either in a .js file you include or enclosed in tags:
function numChecker(e)
{
if (!e.value.match(/^[0-9]+$/))
{
e.value = e.value.substring(0.e.value.length -1 );
e.focus();
return false;
}
return true;
}
function charChecker(e)
{
if (!e.value.match(/^[a-zA-Z0-9\ ]+$/))
{
e.value = e.value.substring(0.e.value.length-1);
e.focus();
return false;
}
return true;
}
Then your input or cfinput fields would have OnKeyUp="numChecker(this)" or OnKeyUp="charChecker(this)" in their attributes.
As people type, if they enter an invalid character this script will kick in and simply remove that bad character. No extra buttons to click or alerts to dismiss.