This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Emulating SQL LIKE in JavaScript
Is there an operator in JavaScript which is similar to the like operator in SQL? Explanations and examples are appreciated.
You can use regular expressions in Javascript to do pattern matching of strings.
For example:
var s = "hello world!";
if (s.match(/hello.*/)) {
// do something
}
The match() test is much like WHERE s LIKE 'hello%' in SQL.
No.
You want to use: .indexOf("foo") and then check the index. If it's >= 0, it contains that string.
Use the string objects Match method:
// Match a string that ends with abc, similar to LIKE '%abc'
if (theString.match(/^.*abc$/))
{
/*Match found */
}
// Match a string that starts with abc, similar to LIKE 'abc%'
if (theString.match(/^abc.*$/))
{
/*Match found */
}
You can check the String.match() or the String.indexOf() methods.
No there isn't, but you can check out indexOf as a starting point to developing your own, and/or look into regular expressions. It would be a good idea to familiarise yourself with the JavaScript string functions.
EDIT: This has been answered before:
Emulating SQL LIKE in JavaScript
No, there isn't any.
The list of comparison operators are listed here.
Comparison Operators
For your requirement the best option would be regular expressions.
Related
First off I been searching the web for this solution.
How to:
<''.split('');
> ['','','']
Simply express of what I'll like to do. But also with other Unicode characters like poo.
As explained in JavaScript has a Unicode problem, in ES6 you can do this quite easily by using the new ... spread operator. This causes the string iterator (another new ES6 feature) to be used internally, and because that iterator is designed to deal with code points rather than UCS-2/UTF-16 code units, it works the way you want:
console.log([...'💩💩']);
// → ['💩', '💩']
Try it out here: https://babeljs.io/repl/#?experimental=true&evaluate=true&loose=false&spec=false&code=console.log%28%0A%20%20%5B%2e%2e%2e%27%F0%9F%92%A9%F0%9F%92%A9%27%5D%0A%29%3B
A more generic solution:
function splitStringByCodePoint(string) {
return [...string];
}
console.log(splitStringByCodePoint('💩💩'));
// → ['💩', '💩']
for ... of could loop through string contains unicode characters,
let string = "😀😃😄😁😆😅🤣😂🙂🙃😉😊😇"
for(var c of string)
console.log(c);
The above solutions work well for simple emojis, but not for the one from an extended set and the ones that use Surrogate Pairs
For example:
splitStringByCodePoint("❤️")
// Returns: [ "❤", "️" ]
To handle these cases properly you'll need a purpose-built library, like for example:
https://github.com/dotcypress/runes
https://github.com/essdot/spliddit
First off I been searching the web for this solution.
How to:
<''.split('');
> ['','','']
Simply express of what I'll like to do. But also with other Unicode characters like poo.
As explained in JavaScript has a Unicode problem, in ES6 you can do this quite easily by using the new ... spread operator. This causes the string iterator (another new ES6 feature) to be used internally, and because that iterator is designed to deal with code points rather than UCS-2/UTF-16 code units, it works the way you want:
console.log([...'💩💩']);
// → ['💩', '💩']
Try it out here: https://babeljs.io/repl/#?experimental=true&evaluate=true&loose=false&spec=false&code=console.log%28%0A%20%20%5B%2e%2e%2e%27%F0%9F%92%A9%F0%9F%92%A9%27%5D%0A%29%3B
A more generic solution:
function splitStringByCodePoint(string) {
return [...string];
}
console.log(splitStringByCodePoint('💩💩'));
// → ['💩', '💩']
for ... of could loop through string contains unicode characters,
let string = "😀😃😄😁😆😅🤣😂🙂🙃😉😊😇"
for(var c of string)
console.log(c);
The above solutions work well for simple emojis, but not for the one from an extended set and the ones that use Surrogate Pairs
For example:
splitStringByCodePoint("❤️")
// Returns: [ "❤", "️" ]
To handle these cases properly you'll need a purpose-built library, like for example:
https://github.com/dotcypress/runes
https://github.com/essdot/spliddit
I'm working on a search function that can show partial result according to your search variable.
For example we are looking for a word "abcde".
We have records of strings like
"ioqwepasaea" "uhabcsdwe" "ewqabcde" "abcfeqs" "cdeeqwee"
So the results should show "uh*abc*sdwe" - "ewq*abcde*"- "*abc*feqs" - "*cde*eqwee"
Is this possible using Jquery? Thank you in advance!!!
Searching for substrings inside strings is done by using regular expressions.
Regular expressions (RegEx) can be used in javascript as well as other languages. It's universal.
RegEx is broadly used for datavalidation but can be used for searching for substrings as well.
There is a lot of guides on the internet on this topic.
I found one for you. take a look :)
http://www.javascriptkit.com/javatutors/re.shtml
As I understand your question, You want to search relating string to your database or etc. You can do it this so many ways. But "jQuery String Functions" are very helpful your solution and also look at Regular Expression. Please try it code below for inspiration related to "string functions".
var myRecords:Array = ["xjavascriptfasf","cavascript","abascript"];
var searchValue = "javascript";
for(var i =0; i<myRecords.length; i++) {
console.log(myRecords[i].search(searchValue));
}
output should be like this :
1
-1
-1
Thats means in your database and user search matched and returned it correct string.
Also please take a look at this "jquery string functions" http://www.jquerybyexample.net/2012/06/jquery-string-functions.html
I hope this is help your trouble.
This question already has answers here:
Javascript equivalent of Perl's \Q ... \E or quotemeta()
(3 answers)
Closed 2 years ago.
In Perl, there's a function named quotemeta which accepts a string and returns a regex pattern that matches that string. It's used in virtually every program to avoid code injection bugs.
One would use quotemeta when dynamically building a pattern. For example,
"^"+quotemeta(var)+"_\\d+$"
A JavaScript implementation follows:
function quotemeta(s) {
return String(s).replace(/\W/g, "\\$&");
}
Given how needed this function when working with regex patterns, I would have expected JavaScript to provide one. Does JavaScript or jQuery already have such a function?
JavaScript doesn't have such a method natively. (And jQuery doesn't include one)
Usually, when searching for a string pattenr, you'd use String.prototype.indexOf. This method find a string in a string, so you won't even need to convert the string pattern to a regex.
String.prototype.replace can also take a String pattern.
It is not exactly the same but it'll work for most string matching use cases.
This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 3 years ago.
I need to make sure a given string is not "Select a value". That's it, anything else should match the pattern, except this exact string.
I've been looking around and trying many combinations on http://www.regular-expressions.info/javascriptexample.html but nothing seems to do the trick.
I can't negate the test, the pattern needs to do it all since I'll feed this to a form validation framework. If the select contains this text, I'll return an error, etc. So the pattern must match anything else, except this exact string,
I tried lots of things,
(?!Select an account)
!(Select an account)
!(^(Select an account)$)
etc... clearly I don't understand how some of these mechanisms work. I get the "starts with" and 'ends with", but I don't seem to find a simple negation operator.
For some reason everywhere I look for regex explanations I don't get this simple use case, maybe it's not common.
How can I accomplish this?
Thank you!
I believe this was asking something similar:
Regular Expressions and negating a whole character group
In your case you could use something like
^(?!Select a value).*$
Which would match everything that does NOT start with "Select a value."
Instead of thinking about a negative regular expression, make a positive one and negate the condition:
if (! /^Select an account$/.test(mystring)) {
// String is not "Select an account"
}
But you don't need regex for this anyway:
if (mystring != 'Select an account') {
// String is not "Select an account"
}
Or if you want "contains":
if (mystring.indexOf('Select an account') == -1) {
// String does not contain "Select an account"
}
I may not be understanding this correctly. Wouldn't this work?
if (string != "Select a value") {
//code here
}
I don't know why you would need regular expressions to do that, which is why I'm afraid I might not be understanding what your question is.
An additional solution, if you’re dealing with regex in HTML5 form validation and the pattern attribute of your input is more complex than one exact string, is as follows (javascript):
function findAllNotPattern(inputField) {
var allBut1 = inputField.getAttribute("pattern")
var allBut2 = allBut1.slice(0, 1) + "^" + allBut1.slice(1)
var allButRe = new RegExp(allBut2, "g")
return allButRe
}
if you want to run it in pure javascript, you'd use:
findAllNotPattern(document.getElementById("inputFieldId"))