I know how to match one pattern using JavaScript:
var pattern = somePattern
if (!pattern.test(email)) { //do something }
but what if I have to match 2 patterns with a space between them so if I have this:
word1 word2
word1 should match pattern1
word2 should match pattern2
a space should be between them
How can I achieve this using JavaScript?
word1word2 is refused (even if they much pattern1 and pattern2 in order, because of the lack of space)
word1 should be an IP; word2 should be a number
Some examples:
172.30.10.10 10 (acceptable)
172.30.10.1010 (not acceptable)
10 10 (not acceptable)
10 172.30.10.10 (not acceptable)
According to the documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/source
The RegExp object has a source property, which returns a string representation of the regex.
Here's an example.
var validName = /\w+ \w+/
var validEmail = /[^# ]+#.+\..+/
var combined = new RegExp(validName.source + ' ' + validEmail.source);
console.log(combined.test('John Doe jdoe#example.com'));
// outputs true
console.log(combined.test('John Doe bademail#'));
// outputs false
However, keep in mind that this solution will NOT work if the regexes include boundary markers like $ and ^.
Combine it into a single pattern:
var pattern = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s+\d{1,}/;
var test= '172.30.10.10 10';
var matched = test.match(pattern);
http://regex101.com/r/kU9cN3/3
UPDATE further to Brian's comment. If they need to be coded as separate patterns you can do as follows. This may be useful if you want to re-use patterns or make other combinations.
var ip = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/,
number = /\d{1,}/,
combinedRegExp = new RegExp(ip.source+' '+number.source);
var testString = '172.30.10.10 10';
var result = combinedRegExp.test(testString);
console.log(result);//true
http://jsfiddle.net/7Lrsxov8/
Building on Joe Frambach's answer, you could perform some sanity checking on the input:
function combinePatterns(pattern1, pattern2, options) {
if (pattern1.constructor != RegExp || pattern2.constructor != RegExp) {
throw '`pattern1` and `pattern2` must be RegExp objects';
}
var pattern1_str = pattern1.source;
var pattern2_str = pattern2.source;
options = options || {};
// Ensure combining the patterns makes something sensible
var p1_endOfLine, p2_startOfLine;
if (pattern1_str.lastIndexOf('$') == pattern1_str.length - 1) {
p1_endOfLine = true;
if (options.stripBadDelimiters || options.swapBadDelimiters) {
pattern1_str = pattern1_str.substr(0, pattern1_str.length - 1);
}
if (options.swapBadDelimiters
&& pattern2_str.lastIndexOf('$') != pattern2_str.length - 1) {
pattern2_str += '$';
}
}
if (pattern2_str.indexOf('^') == 0) {
p2_startOfLine = true;
if (options.stripBadDelimiters || options.swapBadDelimiters) {
pattern2_str = pattern2_str.substr(1);
}
if (options.swapBadDelimiters && pattern1_str.indexOf('^') != 0) {
pattern1_str = '^' + pattern1_str;
}
}
if (p1_endOfLine && p2_startOfLine && options.swapPatterns) {
var tmp = pattern1_str;
pattern1_str = pattern2_str;
pattern2_str = tmp;
}
return new RegExp(pattern1_str + ' ' + pattern2_str);
}
var first = combinePatterns(/abc/, /123/);
var second = combinePatterns(/abc$/, /^123/);
var third = combinePatterns(/abc$/, /^123/, { stripBadDelimiters: true });
var fourth = combinePatterns(/abc$/, /^123/, { swapBadDelimiters: true });
var fifth = combinePatterns(/abc$/, /^123/, { swapPatterns: true });
// first = /abc 123/
// second = /abc$ ^123/
// third = /abc 123/
// fourth = /^abc 123$/
// fourth = /^123 abc$/
This isn't the end-all be-all of what you can do to help ensure your input produces the desired output, but it should illustrate the sorts of possibilities that are open to you when reconstructing the regex pattern in this fashion.
A bit more compact and complete solution:
var ipNum = "\\d{1,3}";
var ip = "(" + ipNum + "\\.){3}" + ipNum;
var num = "\\d+";
var ipRE = new RegExp("^\\s*" + ip + "\\s+" + ipNum + "\\s*$");
console.log(ipRE.test(" 172.30.10.10 10 ")); // true
Related
I am working on an autocomplete text input by testing for string similarity, rather than checking for perfect character matches. This way, a dropdown like a datalists would still present the user with suggestions even if they accidentally add an extra character or spell their desired input wrong.
I have a working Javascript file that can compare the string input from an HTML text input to all the strings in a JSON file that holds about 700 school names as strings. The Javascript file then formats the HTML and passes the 10 most similar strings into an unordered list(for debugging) and into a data-list (where the user will be able to pick their correct answer).
However, datalists seem to have built-in autocomplete that check for identical groups of characters and the datalists will intelligently remove suggestions if the inputted string does not exist within the suggestion.
<input
type ="text"
id="search"
list="hsDropdown"
class ="form-control form-control-lg"
placeholder="High School Name"
autocomplete="off"
autofocus = "false"
/>
<hr/>
<p id="word"></p>
<datalist id ="hsDropdown"></datalist>
<ul id ="list"></ul>
</main>
<script src="js/script.js" type ="text/javascript"></script>
<script src="js/ukkonen/index.js" type ="text/javascript"></script>
The options within the datalist in my HTML are properly populated by my script.js with the most similar strings, but I need to find a way to override the property of the datalist tag that causes results with nonperfect matches to not appear, or
I would need to find an alternative way to make a dropdown list appear from a textbox that is not limited to hard auto-correct.
You could look at the select2 jQuery plugin and the Fuzzy search issue opened there
As per requestor, he has implemented the fuzzy_match function and embedded it into the plugin as the following:
I've also a function called matcher, which looks something like:
function matcher(term, text){
if(term.term === undefined){
return {text: text, score: 1};
}
var match = fuzzy_match(term.term, text.text);
return (match[0])?{text: text, score: match[1]}:false;
}
I also have a sorter, which sorts the matched elements, (so matching elements come at top)
function sorter(data) {
return data.filter(function(item) {
return !!item;
}).sort((a, b) => b.score - a.score)
.map(item => item.text);
}
And whenever we're invoking a select2 on a element, we're passing this matcher as a matcher option, and sorter as sorter option, which looks something like:
$("#element").select2({
placeholder: 'select a name',
matcher,
sorter
})
Here is the fuzzy_match function code provided:
/**
*
* #param pattern
* #param str
* #returns {[boolean,score,formatted]}
*/
function fuzzy_match(pattern, str) {
// Score consts
var adjacency_bonus = 55; // bonus for adjacent matches
var separator_bonus = 10; // bonus if match occurs after a separator
var camel_bonus = 10; // bonus if match is uppercase and prev is lower
var leading_letter_penalty = -3; // penalty applied for every letter in str before the first match
var max_leading_letter_penalty = -9; // maximum penalty for leading letters
var unmatched_letter_penalty = -1; // penalty for every letter that doesn't matter
// Loop variables
var score = 0;
var patternIdx = 0;
var patternLength = pattern.length;
var strIdx = 0;
var strLength = str.length;
var prevMatched = false;
var prevLower = false;
var prevSeparator = true; // true so if first letter match gets separator bonus
// Use "best" matched letter if multiple string letters match the pattern
var bestLetter = null;
var bestLower = null;
var bestLetterIdx = null;
var bestLetterScore = 0;
var matchedIndices = [];
// Loop over strings
while (strIdx != strLength) {
var patternChar = patternIdx != patternLength ? pattern.charAt(patternIdx) : null;
var strChar = str.charAt(strIdx);
var patternLower = patternChar != null ? patternChar.toLowerCase() : null;
var strLower = strChar.toLowerCase();
var strUpper = strChar.toUpperCase();
var nextMatch = patternChar && patternLower == strLower;
var rematch = bestLetter && bestLower == strLower;
var advanced = nextMatch && bestLetter;
var patternRepeat = bestLetter && patternChar && bestLower == patternLower;
if (advanced || patternRepeat) {
score += bestLetterScore;
matchedIndices.push(bestLetterIdx);
bestLetter = null;
bestLower = null;
bestLetterIdx = null;
bestLetterScore = 0;
}
if (nextMatch || rematch) {
var newScore = 0;
// Apply penalty for each letter before the first pattern match
// Note: std::max because penalties are negative values. So max is smallest penalty.
if (patternIdx == 0) {
var penalty = Math.max(strIdx * leading_letter_penalty, max_leading_letter_penalty);
score += penalty;
}
// Apply bonus for consecutive bonuses
if (prevMatched)
newScore += adjacency_bonus;
// Apply bonus for matches after a separator
if (prevSeparator)
newScore += separator_bonus;
// Apply bonus across camel case boundaries. Includes "clever" isLetter check.
if (prevLower && strChar == strUpper && strLower != strUpper)
newScore += camel_bonus;
// Update patter index IFF the next pattern letter was matched
if (nextMatch)
++patternIdx;
// Update best letter in str which may be for a "next" letter or a "rematch"
if (newScore >= bestLetterScore) {
// Apply penalty for now skipped letter
if (bestLetter != null)
score += unmatched_letter_penalty;
bestLetter = strChar;
bestLower = bestLetter.toLowerCase();
bestLetterIdx = strIdx;
bestLetterScore = newScore;
}
prevMatched = true;
}
else {
// Append unmatch characters
formattedStr += strChar;
score += unmatched_letter_penalty;
prevMatched = false;
}
// Includes "clever" isLetter check.
prevLower = strChar == strLower && strLower != strUpper;
prevSeparator = strChar == '_' || strChar == ' ';
++strIdx;
}
// Apply score for last match
if (bestLetter) {
score += bestLetterScore;
matchedIndices.push(bestLetterIdx);
}
// Finish out formatted string after last pattern matched
// Build formated string based on matched letters
var formattedStr = "";
var lastIdx = 0;
for (var i = 0; i < matchedIndices.length; ++i) {
var idx = matchedIndices[i];
formattedStr += str.substr(lastIdx, idx - lastIdx) + "<b>" + str.charAt(idx) + "</b>";
lastIdx = idx + 1;
}
formattedStr += str.substr(lastIdx, str.length - lastIdx);
var matched = patternIdx == patternLength;
return [matched, score, formattedStr];
}
I'm moving up from Photoshop scripts to something else:
Using JavaScript I need to check the validity of website address for three things.
A dot (.) before and after "somesite"
Nothing between ".somesite" and ".com/"
Nothing between ".com" and the "/"
In my example I'm looking at www.somesite.com
Here's the code so far:
URIArr = [
"https://www.somesite.com/find-work-home",
"www.somesite.com/",
"blahsomesite.com/bananas/stuff",
"something.somesite.com/bananas/cheese",
"blahsomesite.com/bananas/123",
"www.blah.somesite.m.com/bananas/5678",
"blah.somesite.comm/bananas/ook",
]
for (var i = 0; i < URIArr.length; i++)
{
var temp = URIArr[i];
var valid = checkURL(".somesite", temp);
if (!valid)
{
alert(temp + " is " + checkURL(".somesite", temp));
}
}
function removeTrailingSlashes(site)
{
return site.replace(/\/$/, "");
}
function checkURL(webstr, str)
{
// A dot (.) before and after "somesite"
// Nothing between ".somesite" and ".com/"
// Nothing between ".com" and the "/"
var test1 = false;
var test2 = false;
var test3 = false;
var c = ".com";
var haystack = str.toLowerCase();
var needle = webstr.toLowerCase();
haystack = removeTrailingSlashes(haystack);
if (!haystack.charAt(haystack .length) === "/")
haystack += "/";
var n = haystack.indexOf(needle);
var m = n + (needle.length);
// first check
if (str.charAt(n) && str.charAt(m) === ".") test1 = true;
//second check
var o = haystack.indexOf(c);
if (o-m === 0) test2 = true;
// third check
var p = o + (c.length);
var truncStr = haystack.substring(o, haystack.length);
var q = truncStr.indexOf("/") + o;
if (q-p === 0) test3 = true;
// final triplecheck
if ((test1 == true) && (test2 == true) && (test3== true)) return true
return false
}
The question is this:
- Did I miss any tricks (I noticed that for the third condition I had to add trailing slashes - even though they might not be present)
But more importantly:
- Could this this be reworked with (three) regular expressions?
Is this a job for Reginald X. Pression?
You can use the following single regex for all three tests:
\.somesite(?=\.com\/)
Js code:
var regex = /\.somesite(?=\.com\/)/g;
var valid = regex.test(myString); //true if found.. else false
See DEMO
Hmm I feel like your three conditions of:
A dot (.) before and after "somesite"
Nothing between ".somesite" and ".com/"
Nothing between ".com" and the "/"
can be simplify to 1 condition:
.somesite.com/
And if that is so, actually without using regex, you can solve it via:
var valid = somestring.indexOf(".somesite.com/") > -1;
using regex would be:
var valid = somestring.match(/\.somesite\.com\//);
In the below code Im not getting the right result. How can I can do pattern match in javascript?
function getPathValue(url, input) {
console.log("this is path key :"+input);
url = url.replace(/%7C/g, '|');
var inputarr = input.split("|");
if (inputarr.length > 1)
input = '\\b' + inputarr[0] + '\n|' + inputarr[1] + '\\b';
else
input = '\\b' + input + '\\b';
var field = url.search(input);
var slash1 = url.indexOf("/", field);
var slash2 = url.indexOf("/", slash1 + 1);
if (slash2 == -1)
slash2 = url.indexOf("?");
if (slash2 == -1)
slash2 = url.length;
console.log("this is path param value :"+url.substring(slash1 + 1, slash2));
return url.substring(slash1 + 1, slash2);
}
getPathValue("http://localhost/responsePath/mountainwithpassid|accesscode/100/mountainwithpassid|passid/1","mountainwithpassid|passid")
Im getting the below output
If I pass mountainwithpassid|accesscode as input Im getting output as
100. Same way if I pass
key :mountainwithpassid|passid value :100 // Expected output 1
If your intention is to simply retrieve the value in the path that follows the input (contained within '/') then you can achieve this with a simpler regular expression. First you will need a method to escape your input string since it contains a pipe character '|' which is translated as OR in regex.
You can use this (taken from https://stackoverflow.com/a/3561711):
RegExp.escape= function(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
Then your getPathValue function can look something like:
function getPathValue(url, input) {
var pathValue = null;
var escapedInput = RegExp.escape(input);
// The RegExp below extracts the value that follows the input and
// is contained within '/' characters (the last '/' is optional)
var pathValueRegExp = new RegExp(".*" + escapedInput + "/([^/]+)/?.*", 'g');
if (pathValueRegExp.test(url)) {
pathValue = url.replace(pathValueRegExp, '$1');
}
return pathValue;
}
You will also need to think about how you handle errors - in the example a null value is returned if no match is found.
I'm trying to understand the question. Given a URL of:
"http://localhost/responsePath/mountainwithpassid|accesscode/100/mountainwithpassid|passid/1"
and an argument of:
"mountainwithpassid|passid"
you expect a return value of:
"1"
An argument of
"mountainwithpassid|accesscode"
should return:
"100"
Is that correct? If so (and I'm not certain it is) then the following may suit:
function getPathValue(url, s) {
var x = url.indexOf(s);
if (x != -1) {
return url.substr(x).split('/')[1];
}
}
var url = "http://localhost/responsePath/mountainwithpassid|accesscode/100/mountainwithpassid|passid/1";
var x = "mountainwithpassid|passid";
var y = "mountainwithpassid|accesscode";
console.log(getPathValue(url, x)); // 1
console.log(getPathValue(url, y)); // 100
I have an expression say
log(1,3)+4,5+max(7,8,9)
where comma is being used two ways.
1- In "log(1,3)+4,5" comma is being used in place of dot(.) or decimal sign.i.e. "log(1,3)+4,5" is equivalent to "log(1.3)+4.5".
2- In max(7,8,9) it is being used as number separator. i.e. this outcome of this is 9 ; the maximum number.
My problem is to substitute comma; which is being used as decimal separator; with decimal but this should not affect max(7,8,9). i.e. I need to convert above expression to
log(1.3)+4.5+max(7,8,9)
What I tried-
function substitute(expr) {
expr.replace(/,/g, function ($`) {
/*some processing here to decide whether comma to be substituted with dot or not.On that basis I will return either dot or comma.*/
}
But how can I pass $` value to associated function
or
Is it possible to do this in javascript.
expr.replace(/,/g,function ($`) {
if yes then how?
Your language is ambiguous.
max(8,1,8,2)
Does this return 8, 8,1 or 8,2?
Your language also doesn't look regular, so you can't parse it with a regular expression, you need the context. If something like this is allowed:
max(1,max(2,3)) // 3?
Assuming you can get rid of the ambiguity, you could write a parser to do the context detection.
This could be a solution :
function myFilter(string) {
// save all functions and signs
var functions = [];
var regExp = /[+,-]max\(([^\)]+)\)/;
matches = true;
while (matches !== null) {
var matches = regExp.exec(string);
if (matches !== null) {
functions.push(matches[0]);
string = string.replace(matches[0], '');
}
}
// replace all remaining commas with dots
string = string.replace(/,/g , ".");
for (i in functions) {
string += functions[i];
}
return string;
}
var s = '1,3+4,5+max(7,8,9)-max(2,3,5)';
var filteredString = myFilter(s);
jsFiddle Demo
This currently works with multiple max functions but only + and - signs. It could be improved with *, / and more... You will have to find the good regex.
Try the below using Javascript. Hope this helps you in logic.
DEMO HERE
var value = "log(1,3)-4,5+max(7,8,9)";
var val = '';
var splitValue, appendSym;
if (value.indexOf("+") != -1)
{
splitValue = value.split("+");
appendSym = "+";
}
else if(value.indexOf("-") != -1)
{
splitValue = value.split("-");
appendSym = "-";
}
else if(value.indexOf("*") != -1)
{
splitValue = value.split("*");
appendSym = "*";
}
else
{
splitValue = value.split("/");
appendSym = "/";
}
var length = splitValue.length;
for (var i = 0; i < length; i++) {
if (val) val += appendSym;
var strrep = splitValue[i].replace(/,/g,".");
if (splitValue[i].indexOf("max") != -1 || splitValue[i].indexOf("min") != -1)
{
val+=splitValue[i];
}
else
{
val+=strrep;
}
}
alert(val);
The output for the above code is log(1.3)-4.5+max(7,8,9)
I have a file full with text in the following format:
(ignoring the fact that it is CSS) I need to get the string between the two | characters and each time, do something:
<div id="unused">
|#main|
#header|
.bananas|
#nav|
etc
</div>
The code I have is this:
var test_str = $('#unused').text();
var start_pos = test_str.indexOf('|') + 1;
var end_pos = test_str.indexOf('|',start_pos);
var text_to_get = test_str.substring(start_pos,end_pos);
//I want to do something with each string here
This just gets the first string. How can I add logic in there to do something for each string?
You can use split method to get array of strings between |
Live Demo
arr = $('#unused').text().split('|');
You can split like
var my_splitted_var = $('#unused').text().split('|');
One way;
$.each($("#unused").text().split("|"), function(ix, val) {
val = $.trim(val); //remove \r|\n
if (val !== "")
alert(val);
});
One way :
var test_str = $('#unused').text();
while(!test_str.indexOf('|'))
{
var start_pos = test_str.indexOf('|') + 1;
var end_pos = test_str.indexOf('|',start_pos);
var text_to_get = test_str.substring(start_pos,end_pos);
test_str = test_str.slice(end_pos,test_str.length);
}
RegExp-Version:
LIVE DEMO (jsfiddle.net)
var trimmedHtml = $("#unused").html().replace(/\s/g, '');
var result = new Array();
var regExp = /\|(.+?)(?=\|)/g;
var match = regExp.exec(trimmedHtml);
result.push(match[1]);
while (match != null) {
match = regExp.exec(trimmedHtml);
if (match != null) result.push(match[1]);
}
alert(result);
So you only get the elements BETWEEN the pipes (|).
In my example I pushed every matching result to an array. You can now iterate over it to get your result.