简体   繁体   English

如何在 jquery/javascript 中剪切包含“map”的字符串

[英]how can I cut a string that contain “map” in jquery/javascript

Does anyone know how can I cut a string that contain "map" in jquery/javascript有谁知道如何在 jquery/javascript 中剪切包含“map”的字符串

Example: If i have a string示例:如果我有一个字符串

var stringName = "abc mapTest bbc";

How Can I cut the string by result only mapTest?如何仅通过结果 mapTest 剪切字符串?

NOTE: like assume the stringName is not the fixed value, it could change to "mapTest asdasd asdd", "asdasd asfffd mapTest" and so on.注意:假设 stringName 不是固定值,它可以更改为“mapTest asdasd asdd”、“asdasd asfffd mapTest”等。 and the "mapTest" will keep changing (eg mapTest1, mapTest, mapTest5 and so on)并且“mapTest”会不断变化(例如mapTest1、mapTest、mapTest5等)

I don't know if you want to remove mapTest (or mapZZPQ or whatever), or return only mapTest when you find map .我不知道您是要删除mapTest (或mapZZPQ或其他),还是在找到mapTest时只返回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, ie /\bmap\w*\b/ .如果您只想匹配单词开头的"map" ,请从模式中删除第一个\w* ,即/\bmap\w*\b/

Given:鉴于:

var stringName = "abc mapTest bbc";
var m = stringName.match(/\bmap\w+\b/);

ie match a word boundary, "map" followed by one or more[*] alphanumerics, and another word boundary.即匹配一个单词边界,“map”后跟一个或多个[*] 字母数字,以及另一个单词边界。

The result you want ("mapTest") will be in您想要的结果(“mapTest”)将在

m[0]

When there's a match, String.match() returns an array, the first element of which is the matched value.当有匹配时, String.match()返回一个数组,其中第一个元素是匹配的值。 If there's no match it'll return null instead.如果没有匹配,它将返回null

[*] if "map" on its own should also match then change the + in the regexp to * . [*] 如果“map”本身也应该匹配,则将正则表达式中的+更改为*

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM