简体   繁体   English

使用javascript,如何使字符串只有字母、数字和句点。 删除所有其他字符

[英]with javascript, how to make string only have letters, numbers, and period. Remove all other characters

What to edit in the js below, so this file name: old_file_name = this8_ file-name=44.jpg下面js里面要编辑什么,所以这个文件名: old_file_name = this8_ file-name=44.jpg
becomes this: new_file_name = this8filename44.jpg变成这样: new_file_name = this8filename44.jpg

Replace any character unless it is a letter, number, or .替换任何字符,除非它是字母、数字或 . with '' (ie. nothing)与 ''(即没有)

var old_file_name = file.upload.filename;
new_file_name = old_file_name.replace(/^[ A-Za-z0-9.\s]*$/i, '');
console.log("file name is:"+new_file_name);
console.log("original file name is:"+old_file_name);

simply this :简单地这个:

 const cleanFileName = fn => fn.replace(/[^a-z0-9.]/ig, '') ; let old_file_name = "this8_ file-Name=44.jpg" , new_file_name = cleanFileName( old_file_name ) ; console.log( old_file_name ) // -> this8_ file-Name=44.jpg console.log( new_file_name ) // -> this8fileName44.jpg

new_file_name = old_file_name.replace(^[A-Za-z0-9_.]+$, '');

这将从头到尾检查它是否匹配一个或多个条件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript删除字符串中不是数字,字母和空格的所有字符 - Javascript remove all characters from string which are not numbers, letters and whitespace 删除字符串中不是字母或数字的所有字符 - Remove all characters that are not letters or numbers in a String 如何使用javascript删除字符串中指定单词之后和句点之前的所有字符? - How can I remove all characters after a specified word and before a period in a string using javascript? 正则表达式JavaScript-如何检查字符串是否全是字母,然后是所有数字 - Regex JavaScript - how to check if a string is all letters and then all numbers Javascript中仅包含数字且在除数字以外的任何位置均不得包含任何字符的字符串的正则表达式 - Regular expression in Javascript for a string which have ONLY numbers & should not have any characters at any position except numbers 使用 Javascript 验证至少 8 个字符和仅字母和数字的组合 - Input validation of at least 8 characters and only combinations of letters and numbers using Javascript 如何用Javascript制作一个识别字母和数字的字段? - How to make a field that recognizes letters and numbers with Javascript? Javascript正则表达式删除字母或空格以外的任何字符 - Javascript Regular Expression to remove any characters other than letters or space 如何确定字符串是否只有字母、数字和下划线字符? - How do I find if a string has only letters, numbers and underscore characters? 从变量中删除除句点之外的所有字母? - Remove all letters from a variable except the period?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM