Regex JavaScript (строка должна включать только альфу, пространство, дефис),

Мне понравилась чистота 110j выше, таким образом, я взял большую часть из нее и осуществил рефакторинг для решения этих 3 проблем, которые я имел с нею:

  1. регулярное выражение соответствовало 'домашнему' URL против всех других
  2. , мне было нужно несколько URL, отображенных на одной вкладке навигации, таким образом, мне был нужен более сложный тег, который берет переменную сумму параметров
  3. , решил некоторые проблемы URL

Здесь, это:

tags.py:

from django import template

register = template.Library()

@register.tag
def active(parser, token):
    args = token.split_contents()
    template_tag = args[0]
    if len(args) < 2:
        raise template.TemplateSyntaxError, "%r tag requires at least one argument" % template_tag
    return NavSelectedNode(args[1:])

class NavSelectedNode(template.Node):
    def __init__(self, patterns):
        self.patterns = patterns
    def render(self, context):
        path = context['request'].path
        for p in self.patterns:
            pValue = template.Variable(p).resolve(context)
            if path == pValue:
                return "active" # change this if needed for other bootstrap version (compatible with 3.2)
        return ""

urls.py:

urlpatterns += patterns('',
    url(r'/ 

base.html:

{% load tags %}

{% url home_url_name as home %}
{% url services_url_name as services %}
{% url contact_url_name as contact %}
{% url contact2_url_name as contact2 %}

<div id="navigation">
    <a class="{% active request home %}" href="home">Home</a>
    <a class="{% active request services %}" href="services">Services</a>
    <a class="{% active request contact contact2 %}" href="contact">Contact</a>
</div>
, view_home_method, {}, name='home_url_name'), url(r'/services/

base.html:

{% load tags %}

{% url home_url_name as home %}
{% url services_url_name as services %}
{% url contact_url_name as contact %}
{% url contact2_url_name as contact2 %}

<div id="navigation">
    <a class="{% active request home %}" href="home">Home</a>
    <a class="{% active request services %}" href="services">Services</a>
    <a class="{% active request contact contact2 %}" href="contact">Contact</a>
</div>
, view_services_method, {}, name='services_url_name'), url(r'/contact/

base.html:

{% load tags %}

{% url home_url_name as home %}
{% url services_url_name as services %}
{% url contact_url_name as contact %}
{% url contact2_url_name as contact2 %}

<div id="navigation">
    <a class="{% active request home %}" href="home">Home</a>
    <a class="{% active request services %}" href="services">Services</a>
    <a class="{% active request contact contact2 %}" href="contact">Contact</a>
</div>
, view_contact_method, {}, name='contact_url_name'), url(r'/contact/

base.html:

{% load tags %}

{% url home_url_name as home %}
{% url services_url_name as services %}
{% url contact_url_name as contact %}
{% url contact2_url_name as contact2 %}

<div id="navigation">
    <a class="{% active request home %}" href="home">Home</a>
    <a class="{% active request services %}" href="services">Services</a>
    <a class="{% active request contact contact2 %}" href="contact">Contact</a>
</div>
, view_contact2_method, {}, name='contact2_url_name'), )

base.html:

{% load tags %}

{% url home_url_name as home %}
{% url services_url_name as services %}
{% url contact_url_name as contact %}
{% url contact2_url_name as contact2 %}

<div id="navigation">
    <a class="{% active request home %}" href="home">Home</a>
    <a class="{% active request services %}" href="services">Services</a>
    <a class="{% active request contact contact2 %}" href="contact">Contact</a>
</div>
17
задан Kyle Hayes 11 September 2009 в 20:31
поделиться

2 ответа

Если вы ищете проверку на достоверность:

// from string start to end, only contains '-' "whitespace" or 'a'-'z' 
someString.match(/^[-\sa-zA-Z]+$/) 

Или отрицание:

// has some invalid character not '-' "whitespace" or 'a'-'z'
someString.match(/[^-\sa-zA-Z]/) 
34
ответ дан 30 November 2019 в 12:20
поделиться
if(someString.match(/[a-z -]+/i){
    // it's valid
}
-2
ответ дан 30 November 2019 в 12:20
поделиться
Другие вопросы по тегам:

Похожие вопросы: