admin管理员组文章数量:1290956
I have a jQuery function with a user input text variable sel
. I display the variable like this:
jQuery(this).parent().prev().find('.js-trigger-add-tag-target').text(sel);
Works fine.
If the variable begins or ends with any of these characters:
.,?!'"
I want to to strip those very characters. For example, if sel
is:
'Hello world, josh's car.,'
I want to strip it to:
Hello world, josh's car
How do I do this in jQuery/javascript? Maybe regex?
I have a jQuery function with a user input text variable sel
. I display the variable like this:
jQuery(this).parent().prev().find('.js-trigger-add-tag-target').text(sel);
Works fine.
If the variable begins or ends with any of these characters:
.,?!'"
I want to to strip those very characters. For example, if sel
is:
'Hello world, josh's car.,'
I want to strip it to:
Hello world, josh's car
How do I do this in jQuery/javascript? Maybe regex?
Share Improve this question asked Feb 13, 2016 at 12:52 Henrik PettersonHenrik Petterson 7,10421 gold badges80 silver badges157 bronze badges 02 Answers
Reset to default 12 +50You can use String#replace
with RegEx
var regex = /^[.,?!'"]+|[.,?!'"]+$/g;
text = text.replace(regex, '');
RegEx Explanation:
^
: Starts of the line anchor$
: End of the line anchor[.,?!'"]+
: Match any of the characters in the character class[
and]
that occur one or more times in any order|
: OR condition/alteration in the RegExg
: Global flag
RegEx101 Live Demo
var regex = /^[.,?!'"]+|[.,?!'"]+$/g; // Note that you don't need `m` flag here
var resultEl = document.getElementById('output');
document.getElementById('myInput').addEventListener('keyup', function() {
resultEl.innerHTML = this.value.replace(regex, '');
}, false);
<input type="text" id="myInput" placeholder="Type something" />
<div id="output">You'll see output here</div>
From OP's ment
How do I adjust the regex so that it removes characters if the variable ends with
's
? For example, if the variable is John's, it will bee John
The RegEx can be modified a bit to match 's
at the end of the string.
The character class can be make lazy/non-greedy and adding 's
as optional group at the end of string.
/^[.,?!'"]+|[.,?!'"]*?(?:'s)?$/
Adding ?
in [.,?!'"]*?
will make the match lazy. (?:)
is non-capturing group and adding ?
in (?:'s)?
will make the 's
optional.
RegEx Demo
Or, as @Daniel Cheung said in the ment, you can also use
/^[.,?!'"]+|(?:[.,?!'"]|'s)+$/
RegEx Demo
Update:
To remove spaces \s
can be added in the character class. Or String#trim
can also be used after `replace.
/^[.,?!'"\s]+|[.,?!'"\s]+$/
^^ ^^
RegEx Demo
As stated in ment by @Casimir et Hippolyte, you can use ^[\W_]+|[\W_]+$
to remove all the special characters at the beginning and end of the string.
I'd prefer a no regex solution simply because the decision "tree" within regex engines is much bigger. and especially because the regexes used for trimming contain query characters which lead to backtracking within the regex engine. such engines often pile the pattern into byte-code, resembling machine instructions. the engine then executes the code, jumping from instruction to instruction. when an instruction fails, it then back-tracks to find another way to match the input. ergo a lot more going on than necessary.
Here is a solution that I personally find more readable and is faster.
function trimByChars(string, characters) {
const first = [...string].findIndex(char => !characters.includes(char));
const last = [...string].reverse().findIndex(char => !characters.includes(char));
return string.substring(first, string.length - last);
}
const chars = ["'", '"', ',', '?', '!', '.'];
trimByChars("'Hello world, josh's car.,'", chars);
本文标签: javascriptIf text begins or ends with these charactersremove charactersStack Overflow
版权声明:本文标题:javascript - If text begins or ends with these characters, remove characters - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741505298a2382284.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论