Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match Word that Starts and Ends with

This must be somewhere... but after wasting quite a bit of time, I can't find it: I would like to test a string matching: "in"+ * +"ing".

In other words,
"interesting" should result in true, whereas
"insist" and "string" should fail.

I am only interested in testing a single word, with no spaces.

I know I could do this in two tests, but I really want to do it one. As always, thanks for any help.

like image 400
n as Avatar asked Nov 24 '13 01:11

n as


2 Answers

If you specifically want to match words then try something like this:

/in[a-z]*ing/i

If you want "in" followed by any characters at all followed by "ing" then:

/in.*ing/i

The i after the second / makes it case insensitive. Either way replace the * with + if you want to have at least one character in between "in" and "ing"; * matches zero or more.

Given a variable in a string you could use the regex to test for a match like this:

var str = "Interesting";
if (/in[a-z]*ing/i.test(str)) {
    // we have a match
}

UPDATE

"What if the prefix and suffix are stored in variables?"

Well then instead of using a regex literal as shown above you'd use new RegExp() and pass a string representing the pattern.

var prefix = "in",
    suffix = "ing",
    re = new RegExp(prefix + "[a-z]*" + suffix, "i");
if (re.match("Interesting")) {
    // we have a match
}

All of the regular expressions I've shown so far will match the "in" something "ing" pattern anywhere within a larger string. If the idea is to test whether the entire string matches that mattern such that "interesting" would be a match but "noninterestingstuff" would not (as per stackunderflow's comment) then you need to match the start and end of the string with ^ and $:

/^in[a-z]*ing$/i

Or from variables:

new RegExp("^" + p + "[a-z]*" + s + "$", "i")

Or if you're testing the whole string you don't necessarily need regex (although I find regex simpler):

var str = "Interesting",
    prefix = "in",
    suffix = "ing";
str = str.toLowerCase(); // if case is not important

if (str.indexOf(prefix)===0 && str.endsWith(suffix)){
   // match do something
}

Or for browsers that don't support .endsWith():

if (str.slice(0,prefix.length)===prefix && str.slice(-suffix.length)===suffix)

"What's the best I can read on the subject?"

MDN gives a rundown of regex for JavaScript. regular-expressions.info gives a more general set of tutorials.

like image 135
nnnnnn Avatar answered Oct 18 '22 19:10

nnnnnn


/in.+ing/ // a string that has `in` then at least one character, then `ing`


/in.+ing/.test('interesting'); // true
/in.+ing/.test('insist');      // false
/in.+ing/.test('string');      // false

/in.+ing/.test('ining'); // false, .+ means at least one character is required.
/in.*ing/.test('ining'); // true, .* means zero or more characters are allowed.

If you wanted to constrain things to just one word, you could use the \w word character shorthand.

/in\w+ing/.test('invents tiring') // false, space is not a "word" character.
/in.+ing/.test('invents tiring') // true, dot matches any character, even space
like image 37
Alex Wayne Avatar answered Oct 18 '22 18:10

Alex Wayne