Regular expression. Find three words in the field - javascript

Help make the correct regular expression for the search 3 words into the field. So far I have done so, but I think it's crazy.
var inp = document.getElementsByTagName('input')[0],
button = document.getElementsByTagName('button')[0];
button.onclick = function() {
console.log(inp.value.match(/^([а-яa-z0-9]+ ){2}[а-яa-z0-9]+/i));
};
<input type="text" />
<button>Check</button>

I guess it's easier to split the text and then verify that the element count is as you expect it. You may want to trim the text before to avoid leading and trailing empty strings in the result array.
console.log(inp.value.trim().split(/\s+/))

Related

javascript regex over getAttribute match?

everyone,
I would like to query or save a part of the page.
But I don't know if the coder works.
can somebody tell if it can work like this?
window._activeShopName='';
window._thisButtonHandler=this.getAttribute('data-shop-name').match(/"(.*?)"/);
if (window._thisButtonHandler) window._activeShopName=window._thisButtonHandler[1];
return true;
I have the problem with the match or better said regex?
HTML-Code
<button data-product-id="13992128" data-offer-id="31702737" data-bid-id="1387" data-bid-place="1" data-rank="3" data-shop-name="google.com" class="offer__to-seller-btn js-product-call-to-action js-redirect-click sl-redirect-click">More</button>
Thanks
It looks like you want just want the value of data-shop-name. If that's the only button or the first button on the page, there's 3 ways to get it's value.
var btn = document.querySelector('button');
var x1 = btn.dataset.shopName;
var x2 = btn.dataset['shopName'];
var z = btn.getAttribute('data-shop-name');
console.log(x1);
console.log(x2);
console.log(z);
<button data-product-id="13992128" data-offer-id="31702737" data-bid-id="1387" data-bid-place="1" data-rank="3" data-shop-name="google.com" class="offer__to-seller-btn js-product-call-to-action js-redirect-click sl-redirect-click">More</button>
It's hard to say what you want, but I'll try to explain what is your code doing at this point:
From getAttribute you will receive a string, but quotation marks are not a part of this string (so you should probably remove them from RegExp).
.*? means match any character, 0 or more times, ungreedy. So, if it can match zero symbols and it must be ungreedy, then it matches exactly zero symbols.

Regex to go beyond 10 numbers

Trying to match with regex, it has to be a number and it shouldn't go beyond 10 numbers. Anything else don't show!
match(/[^0-9]|[0-9]{10,80}/)
The first part works, meaning it's NOT a number, but the second part doesn't, if it's above 10.
http://jsfiddle.net/qwtmnuey/1/
I can do it in another way but I want it the regex way.. thank you!
(function($){
$('input').on( 'keypress', function(e){
var char = String.fromCharCode(e.keyCode);
if ( char.match(/[^0-9]|[0-9]{10,80}/) ) {
e.preventDefault();
}
});
})(jQuery)
HTML:
<input type="text">
You're checking if char matches the regex, but you mean to check if the entire contents of the input match your regex. If you add id="myinput" to your <input> and in your javascript you add
var inputdata = document.getElementById("myinput").value;
Then you can check if inputdata matches your regex it does work.
Also you're checking for {10,80} which means between 10 and 80 but you can also change that to {10,} which means 10 or more

How can I ltrim and rtrim select characters in Javascript?

So I am trying to figure out how I can remove a select set of characters on the end of a string. I've tried some general 'solutions' like str.replace or creating a rtrim, but I kept seeing some situation in which it wouldn't work.
Possible inputs might be:
\r\n some random text \r\n
\r\n some random text
some random text \r\n
some random text
Only the first and the third line should be affected by this function.
Basicly I'm looking for a rtrim function that takes as a parameter, the value/character set that should be trimmed.
I think it might be something way too obvious that I don't see, but at this point I feel like I could use some help.
You can use the following piece of code to do that for you:
var a = "\r\n some random text \r\n";
a = a.replace(new RegExp('\r\n$'), '');
Here, $ matches end of input.
You can refer to the regular expressions guide here to find out more about regex in JS.
EDIT:
If you really need a function for this:
var rTrimRegex = new RegExp('\r\n$');
var rTrim = function(input){
return input.replace(rTrimRegex, '');
}
And then use it inside your code maybe like:
var str = 'my name is foo\r\n\r\n';
str = rTrim(str);

HTML5 form validation pattern automatically create a space after first 3 characters?

Using the pattern attribute in HTML forms is it possible to create a space automatically after 3 characters? How can this be accomplished?
pattern="([A-z0-9À-ž\s]){2,}"
The input is just a text field that receives the same data over and over. 3 numbers, 1 space, and then a name. I would like to be able to enter that but get back an extra space after the numbers.
For example:
If I enter: "951 Houston"I would like it to output: "951 Houston" <---extra space after the 3 numbers.
I would like it to be after any characters entered. So if someone were to enter "Houston" it would actually output "Hou ston" Is this possible using the pattern attribute in forms? If so how? If not what is a possible solution? Thanks
A regex can't add a space, so no, this is not possible with just HTML forms. What you would need to do is first extract the word to replace, then use a regex to split the word into a second word after three characters, then add the result back to the DOM:
var textareas = document.getElementsByTagName("textarea");
var str = document.getElementsByTagName("input")[0].value;
var replaced = str.replace(/.{3}/g, function (value, index) {
return value + (index % 5 == 0 ? ' ' : '');
});
textareas[0].value = replaced;
input, textarea {
width:100%
}
<input value="Houston"/>
<textarea></textarea>
In the above example, the string is extracted from the word in question (Houston in an input field, in this case), and replaced contains the string that has been broken into two new words. Simply insert it wherever you would like :)
Hope this helps! :)

Javascript Textbox "Scrubber"

I have not found a good solution: I have a text box in which users need to be able to type specific info into. For example the command might be "9030 OUT FU [1234 TEST]". I need to "scrub" this text box to ensure that the data was typed in exactly this format (caps not necessary). However there are approximately 50 of these different types of commands.
I am fairly new to javascript, but with good direction can understand it. Is this possible with javascript? Once the data is entered into the text box, it will run a function to return some information, and the text box will be clear for the next command. No 2 commands can be entered at the same time. I just need to check the format is 100% accurate for each command. Any help is appreciated, thank you.
<script type="text/javascript">
function scrub(text) {
var commands = new Array{"someCommand","anotherCommand",...};
for (var i = 0; i <= commands.length; i++) {
if (text.value.toLowerCase().equals(commands[i])) {
//command is valid; do something here
} else {
alert("Invalid command");
}
}
text.value = ""; //clears the text box
}
</script>
For your textarea do this:
<textarea onblur="scrub(this);" ...></textarea>
Is there a set of keywords? And can be they be combined only in a certain fashion?
Looks like couple of regex patterns will be able to do the trick.
e.g: to match "9030 OUT FU [1234 TEST]" regex would be: /\d{4} OUT FU \[\d{4}\]/.
OUT FU and can be substituted with \w{3} and \w{2} respectively (unless you do not want any word to be allowed).
Use regular expressions.
html:
<input type="text" id="code" />
<input type="button" value="test" onclick="alert(checkCode())" />
javascript:
function checkCode(){
var code = document.getElementById('code').value;
return code.match(/\d+ \w+ \w+ \[\d+ \w+\]/)!=null ? true : false;
}
http://gskinner.com/RegExr/ is very helpful with regular expressions.
When you say "exactly this format", you have to understand that we have no clue what you mean. There are an infinite number of patterns that could be used to describe your example. The regular expression above will match if the code has a string of numbers, then a word, then another word, then an opening bracket, then a string of numbers, then a word, then a closing bracket.

Categories

Resources