I am not very familiar with regex. I was trying to test if a string ends with another string. The code开发者_如何学C below returns null when I was expecting true. What's wrong with the code?
var id = "John";
var exists ="blahJohn".match(/id$/);
alert(exists);
Well, with this approach, you would need to use the RegExp
constructor, to build a regular expression using your id
variable:
var id = "John";
var exists = new RegExp(id+"$").test("blahJohn");
alert(exists);
But there are plenty ways to achieve that, for example, you can take the last id.length
characters of the string, and compare it with id
:
var id = "John";
var exist = "blahJohn".slice(-id.length) == id; // true
You would need to use a RegExp()
object to do that, not a literal:
var id = "John",
reg = new RegExp(id+"$");
alert( reg.test("blahJon") );
That is, if you do not know the value you are testing for ahead of runtime. Otherwise you could do:
alert( /John$/.test("blahJohn") );
Try this -
var reg = "/" + id + "$/";
var exists ="blahJohn".match(reg);
The nicer way to do this is to use RegExp.test
:
(new RegExp(id + '$')).test('blahJohn'); // true
(new RegExp(id + '$')).test('blahJohnblah'); // false
Even nicer would be to build a simple function like this:
function strEndsWith (haystack, needle) {
return needle === haystack.substr(0 - needle.length);
}
strEndsWith('blahJohn', id); // true
strEndsWith('blahJohnblah', id); // false
var id = new RegExp("John");
var exists ="blahJohn".match(id);
alert(exists);
try this
I like @lonesomeday 's solution, but Im fan of extending the String.prototype in these scenarios. Here's my adaptation of his solution
String.prototype.endsWith = function (needle) {
return needle === this.substr(0 - needle.length);
}
So can be checked with
if(myStr.endsWith("test")) // Do awesome things here.
Tasty...
var id = "John";
(new RegExp(`${id}$`)).test('blahJohn'); // true
(new RegExp(`${id}$`)).test('blahJohna'); // false
`${id}$` is a JavaScript Template strings which will be compiled to 'John$'.
The $
after John in RegExp stands for end of string so the tested string must not have anything after id value (i.e. John) in order to pass the test.
new RegExp(`${id}$`) - will compile it to /John$/
(so if id shouldn't be dynamic you can use just /John$/ instead of new RegExp(`${id}$`) )
Why using RegExp? Its expensive.
function EndsWith( givenStr, subst )
{
var ln = givenStr.length;
var idx = ln-subst.length;
return ( giventStr.subst(idx)==subst );
}
Much easier and cost-effective, is it?
If you need it for replace function, consider this regExp:
var eventStr = "Hello% World%";
eventStr = eventStr.replace(/[\%]$/, "").replace(/^[\%]/, ""); // replace eds with, and also start with %.
//output: eventStr = "Hello% World";
2022, ECMA 11
Just created this helper function, I find it more useful and clean than modifying the regex and recreating one everytime.
/**
* @param {string} str
* @param {RegExp} search
* @returns {boolean}
*/
function regexEndsWith (str, search, {caseSensitive = true} = {})
{
var source = search.source
if (!source.endsWith('$')) source = source + '$'
var flags = search.flags
if (!caseSensitive && !flags.includes('i')) flags += 'i'
var reg = new RegExp(source, flags)
return reg.test(str)
}
Use it this way:
regexEndsWith('can you Fi nD me?', /fi.*nd me?/, {caseSensitive: false})
Here is a string prototype function that utilizes regex. You can use it to check if any string object ends with a particular string value:
Prototype function:
String.prototype.endsWith = function (endString) {
if(this && this.length) {
result = new RegExp(endString + '$').test(this);
return result;
}
return false;
}
Example Usage:
var s1 = "My String";
s1.endsWith("ring"); // returns true;
s1.endsWith("deez"); //returns false;
精彩评论