我需要一个正则表达式,用于过滤字符串中没有任何点的数字。我怎样才能缩短我的表达?
myString.replace( /[^\d.]/g, '' ).replace(/\./g, '')
答案 0 :(得分:2)
使用 public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<User> users = new List<User>();
users.Add(new User() { Id = 1, Name = "John Doe", Birthday = new DateTime(1971, 7, 23) });
users.Add(new User() { Id = 2, Name = "Jane Doe", Birthday = new DateTime(1974, 1, 17) });
users.Add(new User() { Id = 3, Name = "Sammy Doe", Birthday = new DateTime(1991, 9, 2) });
users.Add(new User() { Id = 4, Name = "Sammy Doe1", Birthday = new DateTime(1991, 9, 2) });
users.Add(new User() { Id = 5, Name = "Sammy Doe2", Birthday = new DateTime(1991, 9, 2) });
users.Add(new User() { Id = 6, Name = "Sammy Doe3", Birthday = new DateTime(1991, 9, 2) });
lvDataBinding.ItemsSource = users;
}
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Birthday { get; set; }
public string Details
{
get
{
return String.Format("{0} was born on {1} and this is a long description of the person.", this.Name, this.Birthday.ToLongDateString());
}
}
}
,删除任何不是数字或字面点的字符。
使用.replace( /[^\d.]/g, '' )
,删除所有点。
要合并,只需使用
.replace(/\./g, '')
myString = myString.replace( /\D/g, '' )
将匹配所有不是数字的字符。