获取以特定文本

时间:2017-05-23 16:06:46

标签: javascript

我有字符串

str = 'ab created > 2017-04-22 AND creator ="abc" ORDER BY reporter'

我需要的是,我想删除以ORDER开头的子字符串..

结果:

str = 'ab created > 2017-04-22 AND creator ="abc"'
subString = 'ORDER BY reporter'

5 个答案:

答案 0 :(得分:0)

使用str.indexOf()str.substring()执行此操作,类似于:

var str = "ab created > 2017-04-22 AND creator ='abc' ORDER BY reporter"

var order_str = str.substring(str.indexOf('ORDER') );
var new_str = str.substring(0, str.indexOf('ORDER'));

console.log(order_str,'\n', new_str);

答案 1 :(得分:0)

你需要逃避你的报价,但你可以这样做:

const newStr = str.replace(/ORDER BY.+/, '')

答案 2 :(得分:0)

试试这个:

str = str.substring(0, str.indexOf("ORDER BY reporter"))

返回'ab created > 2017-04-22 AND creator ="abc" '。如果您还需要删除尾随空格,请改为:

str = str.substring(0, str.indexOf("ORDER BY reporter")-1)

答案 3 :(得分:0)

var str = 'ab created > 2017-04-22 AND creator ="abc" ORDER BY reporter'


function SliceMe(origStr,slicer){

var result =origStr.slice(0,origStr.indexOf(slicer));

var result1 =origStr.slice(origStr.indexOf(slicer));

console.log(origStr)
console.log("result: " +result)
console.log("result1 : " + result1)

}

SliceMe(str,"ORDER");

答案 4 :(得分:0)

<script>
    var str = 'ab created > 2017-04-22 AND creator ="abc" ORDER BY reporter';
    console.log(str);
    var subString = str.substring(str.indexOf("ORDER"));
    console.log(subString);
    var str = str.substring(0,str.indexOf("ORDER"));
    console.log(str);
</script>