JS在最后一次出现角色后删除所有内容

时间:2015-07-09 04:42:53

标签: javascript

好的,我有这个

     <plugins>
        <!--Some plugins omitted >
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include> **/*.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include> **/*.xml</include>
            </includes>
        </resource>
    </resources>

给你&#34; /删除 - 在最后一次出现的角色之前的所有内容&#34;

如何获得&#34; var URL = "http://stackoverflow.com/questions/10767815/remove-everything-before-the-last-occurrence-of-a-character"; console.log(URL.substring(URL.lastIndexOf("/"))); &#34;

9 个答案:

答案 0 :(得分:14)

你在这里:

var URL = "http://stackoverflow.com/questions/10767815/remove-everything-before-the-last-occurrence-of-a-character";
alert(URL.substring(0, URL.lastIndexOf("/") + 1));

希望这有帮助。

答案 1 :(得分:2)

var URL = "http://stackoverflow.com/questions/10767815/remove-everything-before-the-last-occurrence-of-a-character";

console.log(URL.substring(0,URL.lastIndexOf('/')+1));
//The +1 is to add the last slash

答案 2 :(得分:2)

似乎是正则表达式的一个好例子(不能相信没有人发布它):

{{1}}

将所有连续的非正斜杠字符移除到字符串的末尾(即最后一个/)之后的所有内容。

答案 3 :(得分:1)

通用解决方案

这是一个通用函数,当在我们要搜索的字符串(干草堆)中找不到所搜索的字符或字符串(针)时,该函数也可以处理边缘情况。在这种情况下,它将返回原始字符串。

function trimStringAfter(haystack, needle) {
  const lastIndex = haystack.lastIndexOf(needle)
  return haystack.substring(0, lastIndex === -1 ? haystack.length : lastIndex + 1)
}

console.log(trimStringAfter('abcd/abcd/efg/ggfbf', '/')) // abcd/abcd/efg/
console.log(trimStringAfter('abcd/abcd/abcd', '/')) // abcd/abcd/
console.log(trimStringAfter('abcd/abcd/', '/')) // abcd/abcd/
console.log(trimStringAfter('abcd/abcd', '/')) // abcd/
console.log(trimStringAfter('abcd', '/')) // abcd

答案 4 :(得分:0)

console.log(URL.substring(0, URL.lastIndexOf("/")+1));

答案 5 :(得分:0)

Try this jsfiddle或运行代码段。

    var URL = "http://stackoverflow.com/questions/10767815/remove-everything-before-the-last-occurrence-of-a-character";
    var myRegexp = /^(.*\/)/g;
    var match = myRegexp.exec(URL);
    alert(match[1]); 

答案 6 :(得分:0)

尝试基于数组的提取,如

&#13;
&#13;
query.equalTo('availability.status', 'busy');
&#13;
var URL = "http://stackoverflow.com/questions/10767815/remove-everything-before-the-last-occurrence-of-a-character";
snippet.log(URL.split('/').slice(0, 5).join('/'));
&#13;
&#13;
&#13;

答案 7 :(得分:0)

运行:

var URL = "http://stackoverflow.com/questions/10767815/remove-everything-before-the-last-occurrence-of-a-character";
var temp = URL.split('/');
temp.pop();
var result = temp.join('/');
alert(result);

答案 8 :(得分:0)

尝试将.match()RegExp /^\w+.*\d+\//

一起使用

var URL = "http://stackoverflow.com/questions/10767815/remove-everything-before-the-last-occurrence-of-a-character";
var res = URL.match(/^\w+.*\d+\//)[0];
document.body.textContent = res;

相关问题