Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript/Angular中限制第一个位置的空间 我有下面的正则表达式,允许单词中间的空格。_Javascript_Regex_String_Function - Fatal编程技术网

如何在javascript/Angular中限制第一个位置的空间 我有下面的正则表达式,允许单词中间的空格。

如何在javascript/Angular中限制第一个位置的空间 我有下面的正则表达式,允许单词中间的空格。,javascript,regex,string,function,Javascript,Regex,String,Function,oninput="this.value = this.value.replace(/[^A-Za-z0-9-,.;'&/.() ]/g,'')" 如何限制输入框中第一个位置的空间。 中间的空格是允许的。 可以使用内置字符串方法 Times()/。javascript中的code>trim()方法从字符串的开头和结尾删除空格 this.value=this.value.trim() //如果您还想应用正则表达式,可以尝试以下代码 this.value=this.value.replace

oninput="this.value = this.value.replace(/[^A-Za-z0-9-,.;'&/.() ]/g,'')"
如何限制输入框中第一个位置的空间。
中间的空格是允许的。

可以使用内置字符串方法<代码> Times()/<代码>。javascript中的code>trim()方法从字符串的开头和结尾删除空格

this.value=this.value.trim()
//如果您还想应用正则表达式,可以尝试以下代码
this.value=this.value.replace(/[^A-Za-z0-9-,.;'&/()]|^/g')。trim()

const a='hello'
console.log(a.trim())

//您好
您可以使用内置字符串方法
trim()
trim()方法从字符串的开头和结尾删除空格

this.value=this.value.trim()
//如果您还想应用正则表达式,可以尝试以下代码
this.value=this.value.replace(/[^A-Za-z0-9-,.;'&/()]|^/g')。trim()

const a='hello'
console.log(a.trim())

//hello
交替使用字符串锚定的开头(使用
^
),后跟空格,以及负数字符集的其余部分:

oninput="this.value = this.value.replace(/[^A-Za-z0-9-,.;'&/.() ]|^ /g,'')"

交替使用字符串锚点的开头(使用
^
),后跟空格,以及负数字符集的其余部分:

oninput="this.value = this.value.replace(/[^A-Za-z0-9-,.;'&/.() ]|^ /g,'')"

只需
修剪
字符串,然后再
替换它:

oninput="this.value = this.value.trim().replace(/[^A-Za-z0-9-,.;'&/.() ]/g,'')"

只需修剪字符串,然后更换它:

oninput="this.value = this.value.trim().replace(/[^A-Za-z0-9-,.;'&/.() ]/g,'')"

这个有点像的,但是保存插入符号的位置,并且不会跳到输入的末尾

编辑 添加了一些视觉反馈

;(() => {
const inp=document.querySelector('#inp');
常量nospaces=函数(elem){
//在输入开始处查找空格
const spacesReg=/^\s+/;
让比赛
if(match=elem.value.match(spacesReg)){
//保存当前插入符号位置
const pos=元素选择开始;
常量len=match[0]。长度;
元素值=元素值替换(空格“,”);
//复位插入符号位置
elem.selectionStart=elem.selectionEnd=Math.min(0,位置-len)
返回真值
}
返回错误
}
仅常量LowedCharacters=函数(元素){
//搜索不允许的字符
const allowed=“A-Za-z0-9-,.;”&/()”;
const notallowereg=new RegExp(`(.*?[^${allowed}]`);
//防止无限循环
var max=255;
让我们比赛;
让闪烁=假
//每个循环运行替换一个不允许的字符
而((max>0)和(&(match=elem.value.match(notAllowedReg))){
闪烁=真
const pos=元素选择开始;
常量len=匹配[1]。长度
elem.value=elem.value.replace(notAllowedReg,$1');
elem.selectionStart=elem.selectionEnd=pos-1
马克斯--
}
返回闪烁;
}
常量闪烁=功能(元素,持续时间=200){
const to=setTimeout(()=>{
elem.classList.remove('blink')
},持续时间)
元素classList.add('blink')
}
//使用函数文字获取正确的'this'值
inp.addEventListener('input',函数(e){
const nosp=nospaces(this);
const only=onlyAllowedCharacters(此);
如果(仅限nosp){
眨眼(这个)
}
})
})();
。闪烁{
背景:红色;
}
输入开头没有空格
仅允许使用字符a–zA–Z0–9-,.;'&\。()

此选项的工作方式有点像,但保存的是插入符号位置,不会跳转到输入的末尾

编辑 添加了一些视觉反馈

;(() => {
const inp=document.querySelector('#inp');
常量nospaces=函数(elem){
//在输入开始处查找空格
const spacesReg=/^\s+/;
让比赛
if(match=elem.value.match(spacesReg)){
//保存当前插入符号位置
const pos=元素选择开始;
常量len=match[0]。长度;
元素值=元素值替换(空格“,”);
//复位插入符号位置
elem.selectionStart=elem.selectionEnd=Math.min(0,位置-len)
返回真值
}
返回错误
}
仅常量LowedCharacters=函数(元素){
//搜索不允许的字符
const allowed=“A-Za-z0-9-,.;”&/()”;
const notallowereg=new RegExp(`(.*?[^${allowed}]`);
//防止无限循环
var max=255;
让我们比赛;
让闪烁=假
//每个循环运行替换一个不允许的字符
而((max>0)和(&(match=elem.value.match(notAllowedReg))){
闪烁=真
const pos=元素选择开始;
常量len=匹配[1]。长度
elem.value=elem.value.replace(notAllowedReg,$1');
elem.selectionStart=elem.selectionEnd=pos-1
马克斯--
}
返回闪烁;
}
常量闪烁=功能(元素,持续时间=200){
const to=setTimeout(()=>{
elem.classList.remove('blink')
},持续时间)
元素classList.add('blink')
}
//使用函数文字获取正确的'this'值
inp.addEventListener('input',函数(e){
const nosp=nospaces(this);
const only=onlyAllowedCharacters(此);
如果(仅限nosp){
眨眼(这个)
}
})
})();
。闪烁{
背景:红色;
}
输入开头没有空格
仅允许使用字符a–zA–Z0–9-,.;'&\。()

您可以调用
this.value=this.value.trim()后跟任何其他要执行的逻辑。我不想在用户输入后修剪。我想限制用户在第一个位置输入空格的时间将其添加到元素
onkeypress=“return event.charCode!==32 | | this.value.length”
您可以调用
this.value=this.value.trim()后跟任何其他要执行的逻辑。我不想在用户输入后修剪。我想限制用户在第一个位置输入空格的时间将其添加到元素
onkeypress=“return event.charCode