Does anyone know how can I cut a string that contain "map" in jquery/javascript
Example: If i have a string
var stringName = "abc mapTest bbc";
How Can I cut the string by result only mapTest?
NOTE: like assume the stringName is not the fixed value, it could change to "mapTest开发者_运维百科 asdasd asdd", "asdasd asfffd mapTest" and so on. and the "mapTest" will keep changing (e.g mapTest1, mapTest, mapTest5 and so on)
I don't know if you want to remove mapTest
(or mapZZPQ
or whatever), or return only mapTest
when you find map
. Here is code to do either:
var stringName = "123 abcmapdef 456"; // sample input
var hasMap = stringName.match(/\b\w*map\w*\b/); // returns "abcmapdef"
var hasNoMap = stringName.replace(/\b\w*map\w*\b/g, ""); // returns "123 456"
If you only want to match "map"
at the beginning of word, remove the first \w*
from the pattern, i.e. /\bmap\w*\b/
.
Given:
var stringName = "abc mapTest bbc";
var m = stringName.match(/\bmap\w+\b/);
i.e. match a word boundary, "map" followed by one or more[*] alphanumerics, and another word boundary.
The result you want ("mapTest") will be in
m[0]
When there's a match, String.match()
returns an array, the first element of which is the matched value. If there's no match it'll return null
instead.
[*] if "map" on its own should also match then change the +
in the regexp to *
.
You want something like this:
var result = stringName.split(" ");
var mapTest = result[1];
Hope that helps.
var stringName = "abc mapTest bbc";
var patt1=/mapTest/gi;
var result = stringName.match(patt1);
精彩评论