regex question mark in javascript - javascript

It is probably quite simple, but I do not how to do.
I have this regex :
new RegExp("^[A-Za-z\\u00C0-\\u017F][\\- ]?+$");
It validates a first name. The name have to begin with a letter (the range is in unicode and works fine) and then continue with letters or - or space. But it can be just letters, as in most names.
I have searched but I didn't find the right way to do it.
I don't want to duplicate the character range. It is just to have a code more "proper".
If you could help, it would be great :)
Thanks in advance

Right now you only allow one letter and then one or more dashes/spaces. You probably want
new RegExp("^[A-Za-z\\u00C0-\\u017F][A-Za-z\\u00C0-\\u017F -]*$");
But in general, trying to validate a name with regexes isn't such a good idea.

Related

How to translate Japanese Kanji to Katakana

The requirement is to:
As the user type his/her Japanese Kanji first and last names,
automatically fill in the corresponding Japanese Katakana first and
last names.
I have been searching for a while now, but I couldn't yet find anything. There seems to be several jQuery plugins that convert Hiragama to Katakana or Romaji or vice-versa but that is not what we need here.
There is one that claims to translate from Kanji to Kana but I don't think the code matches his description (it only executes the code if the input is Kana but that is supposed to be the output!).
Anyway, I need to translate a person's first/last names from Kanji to Kana.
How do I do this?
As this needs to happen while the user is filling the form, I am prefer a JavaScript solution (or any pointers to it) but if there are any pointers how to do this in .NET, I'll very much appreciated too.
It seems like there are several JavaScript solutions online to convert from Kanji, Romaji, Hiragana and Katakana. Check these out and see if they work for you:
JQuery Auto Kana Input
Kuroshiro
jp-conversion
WanaKanaJS

RegExp must have \w+ and \s+ characters

I've been trying to create a RegExp that makes sure a sure has entered at least one word and at least one space. I tried to use this:
/\w+\s+/
But that makes sure that there is a word AFTER a space. I just want to make sure there is both in a string. They don't need to be in the order of the above RegExp.
How can I make the RegExp work, but without matching the order?
/(?=.*?\w)(?=.*?\s)/
?= means "look-ahead", and .* means "any number of characters"
So "find any number of characters then a \w", "find any number of characters and a \s"
Another thing to note about how this works, look-aheads are "non-matching", making it so that this can match in any order.
You have two things:
Is there a word character?
Is there a space?
Two things.
str.match(/\w/)
str.match(/\s/)
So why are you trying to do them as one step?
if( str.match(/\w/) && str.match(/\s/))
There are a lot of answers to my question. However, I do not want to simply pick the one that is upvoted. Please give a detailed explanation of why your regex works, and maybe why mine doesn't.
My answer provides the simplest solution. It is very clear to anyone reading it that we are checking "if it has a word character, and if it contains a space character". It is also very easy to expand on, such as if you want to add another check.
zyklus' answer (/(?=.*?\w)(?=.*?\s)/) is the fastest when speed-tested on a 50Kb string of input. In more common cases (ie. 100 character at most), this speed difference will be practically non-existent. It is twice as fast as my answer, but "2 * very small number = very small number". It's easy enough to add new test cases (just add another (?=.*something) block) but is less humanly-obvious as to what it does.
Jacob's answer ((\w+.*\s+)|(\s+.*\w+)) does quite literally what you asked, checking first if there is a word character and then a space character, then checks the other way around before failing. It works, however it is slower. Furthermore, if you decide to add a new test case, you'd get something like (\w+.*\s+.*\d+)|(\w+.*\d+.*\s)|(\s+.*\w+.*\d+)|(\s+.*\d+.*\w+)|(\d+.*\w+.*\s+)|‌​(\d+.*\s+.*\w+). It only gets worse if you add a fourth test (24 arrangements to check) and is unreadably ugly. Do not use this answer.
Other answers are variants of existing ones.
If you need to do it in one RegEx for some reason:
(\w+.*\s+)|(\s+.*\w+)
Can be handy if you're working with a library that only enables you to use a single regular expression.

Regex to search inside contents of file

I am using software to search inside txt files with Regex, For example
So I want to find files which Contain "Black" under "color" under "clothes", So the regex should highlight "Black" which located in line 7 only.
My aim here is to follow the order no mater how many lines between terms.
I will be more than thankful if you explain "regex" so I know what I have missed.
Thanks in advance
You can use the following regex for matching the given 'Black':
/.+?Clothes.+?Colour.+?(Black)/s
Description
Demo
http://regex101.com/r/gD9iD3
Discussion
The regex here may not fit all your needs. It uses .+? for navigating between the searched elements. This is really imprecise: the "Red" case that you have outputted in your comments is a guenine POC.
The next step here is to determine how elements (Clothes, Colour etc) are structured. Armed with this knowledge, the regex can be strengthened. .+? will be replaced then by more specific elements.
If the regex is used in a Javascript context, the s modifier is not available.
To work around this limitation, the following structure: .+? can be replaced with [\s\S]+?.

Javascript RegExp parse URL make hyperlinks ignore img src

I am not very good with Regular Expressions, some times I can figure them out but...
I need to parse text strings (for a chat room project).
So as you would imagine any pasted URLs need to be converted to click-able hyper links.
I use this RegExp for that, cobbled together from examples I have found on the net. It appears to work quite well :
/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:~;#'#%&.=\]\[\*\$\!\?\/\,]+/g
Now another part of my project has to insert images in other words :
<img src="http://path/to/image" alt="alt" />
So I need the reg exp to ignore those, and I tried this :
/(?!src=")[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:~;#'#%&.=\]\[\*\$\!\?\/\,]+/g
But it doesn't work. Perhaps my expression is faulty or I am going about it the wrong way.
I may just mask out 'src="http' and run my expression then reapply what I masked out.
But before I do that I thought I would see if anyone here has any ideas.
Many thanks.
(?!src=")
is a negative lookahead, what you want there is a lookbehind, which javascript does not support.

Javascript regex syntax for HTML5 input validation

So there have been plenty of questions, and filtering through a few, I still dont know how to go about this...
Pattern for:
Alphabets ONLY, no case sensitivity, no limit on character count or words, minimum 3 characters...
I have
pattern="[A-z]{3,}"
That gives me everything, except that I'm limited to one word only... :-(
Edit: Let me be a little more clear on what I want the validation to achieve for me...
I'm using it to capture a person's name. But I do not want any special characters or numerals involved, so no "John Doe Jr.", as the '.' will get rejected, but I want to be able to capture double, or even single character portions, whilst maintaining 'global' 3 char minimum limit...
All you have to do to allow spaces as well is to add a space to the character pattern where you have [A-z].
So it becomes:
pattern="[A-z ]{3,}"
Hope that helps.
Note, however, that this will prevent other types of white space characters. I assume this is what you want, since you're being quite restrictive with the rest of the character set, but it's worth pointing out that non-breaking spaces, carriage returns, and other white space will be blocked in the above. If you want to allow them, use \s instead of just a space: this will match any white space character.
Finally, it's worth pointing out that the standard alphabet is often insufficient even for plain English text. There are valid English words with accents, as well as apostrophes and other punctuation. You haven't specified what the field is being used for, so I'll assume this is not an issue, but I felt it was worth pointing out nevertheless.
It is difficult to see what is the question. You are matching a String, not a set of words.
If your pattern is "a list of words, each of the words alphabetical only and separated by whitespace", then the regex would be
([A-Za-z]{3,}\\s*)+
Edited to answer to updated question.
[A-Za-z\\s]*([A-Za-z]{3,})+[A-Za-z\\s]* (works in Java)
How about pattern = "[A-z\s]{3,}"?

Categories

Resources