[英]I want to remove special character from string but I need dot in string in javascript
[英]Split the string from character and that character I don't want remove
我想在JS中拆分字符串。 我已經知道函數str.split();
但我想要一些不同的東西。 喜歡:我有字符串var str = "Hello word"
。 我必須從字符o
拆分這些字符串而不是像array = ['hell', 'o' , ' w', 'o','rd']
轉換的字符串: array = ['hell', 'o' , ' w', 'o','rd']
var str = "Hello word"
var ary = str.split("o");
// output : ary = ['hell', ' w' ,'rd'];
// I want : ary = ['hell', 'o' , ' w', 'o','rd'];
請任何人幫助我如何獲得這樣的輸出。
你可以試試這個:
var str = "Hello word"
var ary = str.split(/(o)/));
//ary = ['hell', 'o' , ' w', 'o','rd'];
你真的想要match
,而不是split
。
str = "Hello word" ary = str.match(/o|[^o]+/g) document.write(ary)
split(/(o)/)
也有效,但請注意:
如果separator是包含捕獲括號的正則表達式,則每次匹配時,捕獲括號的結果(包括任何未定義的結果)都會拼接到輸出數組中。 但是,並非所有瀏覽器都支持此功能。 ( https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/split#Capturing_parentheses )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.